Return to Snippet

Revision: 15800
at July 16, 2009 20:13 by bakkelun


Updated Code
-- mysql connection handler --

<?

class mysql {
	var $dbase;
	var $dbcon;     
        var $db_user;
        var $db_pw;

        /* Constructor */
	function __construct($base,$user,$pw) {

	$this->dbase = $base;
	$this->db_user = $user;
	$this->db_pw = $pw;        

	$this->dbcon = mysql_connect("url-to-your-db",$this->db_user,$this->db_pw); 
	if (!$this->dbcon)  {  
		die( "<p>Unable to connect to the database server at this time.</p>" );
		} else { 
			if (!@mysql_select_db($this->dbase,$this->dbcon))  {
			die( "<p>Could not choose this database: ".$this->dbase." Please try again later.</p>" );
			}
		}
	} // connect end
	 

	function closeconnection() {
	  mysql_free_result();
	  mysql_close($this->dbcon);
	} // closeconnection end
	
}
?>


--- sql data object ---

<? 
require('path to the above mysql class file');

class sqlDataObject extends mysql {

	var $resultSet;
	var $update_string;

        /**
	* Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads.
	*
	*/
	function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') {
	
	if (!empty($where)) { $where = 'WHERE '.$where; }
	
	if (!empty($orderby)) { $orderby = 'ORDER BY '.$orderby; }
	
	if (!empty($limit)) { $limit = 'LIMIT '.$offset.','.$limit; }
	
	$query = mysql_query("SELECT $what_to_get FROM $table $where $orderby $sortorder $limit");
	if (!$query) {
		$this->errorHandler(mysql_error());
	}

	// Output as array
	if ($outputArray=='1') {
		while ($query_row = mysql_fetch_assoc($query)) {
			$this->resultSet[] = $query_row;
		}
	
	// Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result
	} else {
		$this->resultSet = $query;
	}
    }

     /* Handle potensially dangerous input */
	function safevalue ($value) {
		return mysql_real_escape_string(strip_slashes(trim($value)));
	}

}
?>

---- EXAMPLE USAGE ----

Let's say we want to output all the info in our 'newstable' from our database 'example-database'. Here's how:

<?
require('path-to-the-sql-data-object-file.php');

$sqlData = new sqlDataObject('example_database','username','password');




/* Get all news info where the id is less than 10 and ordr by the id. */

/* In this example let's say the database has only three columns: id, heading, message */

$some_news_output = $sqlData->getData('newstable','*','id<10','id');

while ($news_row = mysql_fetch_array($some_news_output)) {
   echo('<h1>'.$news_row['heading'].'</h1>'.
    $news_row['message'].'
   <p><a href="?page=news&amp;id="'.$news_row['id'].'">Read more about this</a></p>');
}
?>

Revision: 15799
at July 16, 2009 20:12 by bakkelun


Updated Code
-- mysql connection handler --

<?

class mysql {
	var $dbase;
	var $dbcon;     
        var $db_user;
        var $db_pw;

        /* Constructor */
	function __construct($base,$user,$pw) {

	$this->dbase = $base;
	$this->db_user = $user;
	$this->db_pw = $pw;        

	$this->dbcon = mysql_connect("url-to-your-db",$this->db_user,$this->db_pw); 
	if (!$this->dbcon)  {  
		die( "<p>Unable to connect to the database server at this time.</p>" );
		} else { 
			if (!@mysql_select_db($this->dbase,$this->dbcon))  {
			die( "<p>Could not choose this database: ".$this->dbase." Please try again later.</p>" );
			}
		}
	} // connect end
	 

	function closeconnection() {
	  mysql_free_result();
	  mysql_close($this->dbcon);
	} // closeconnection end
	
}
?>


--- sql data object ---

<? 
require('path to the above mysql class file');

class sqlDataObject extends mysql {

	var $resultSet;
	var $update_string;

        /**
	* Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads.
	*
	*/
	function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') {
	
	if (!empty($where)) { $where = 'WHERE '.$where; }
	
	if (!empty($orderby)) { $orderby = 'ORDER BY '.$orderby; }
	
	if (!empty($limit)) { $limit = 'LIMIT '.$offset.','.$limit; }
	
	$query = mysql_query("SELECT $what_to_get FROM $table $where $orderby $sortorder $limit");
	if (!$query) {
		$this->errorHandler(mysql_error());
	}

	// Output as array
	if ($outputArray=='1') {
		while ($query_row = mysql_fetch_assoc($query)) {
			$this->resultSet[] = $query_row;
		}
	
	// Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result
	} else {
		$this->resultSet = $query;
	}
    }

     /* Handle potensially dangerous input */
	function safevalue ($value) {
		return mysql_real_escape_string(strip_slashes(trim($value)));
	}

}
?>

---- EXAMPLE USAGE ----

Let's say we want to output 

require('path-to-the-sql-data-object-file.php');

$sqlData = new sqlDataObject('example_database','username','password');




/* Get all news info where the id is less than 10 and ordr by the id. */

/* In this example let's say the database has only three columns: id, heading, message */

$some_news_output = $sqlData->getData('newstable','*','id<10','id');

while ($news_row = mysql_fetch_array($some_news_output)) {
   echo('<h1>'.$news_row['heading'].'</h1>'.
    $news_row['message'].'
   <p><a href="?page=news&amp;id="'.$news_row['id'].'">Read more about this</a></p>');
}

Revision: 15798
at July 16, 2009 20:11 by bakkelun


Updated Code
-- mysql connection handler --

<?

class mysql {
	var $dbase;
	var $dbcon;     
        var $db_user;
        var $db_pw;
	function __construct($base,$user,$pw) {

	$this->dbase = $base;
	$this->db_user = $user;
	$this->db_pw = $pw;        

	$this->dbcon = mysql_connect("url-to-your-db",$this->db_user,$this->db_pw); 
	if (!$this->dbcon)  {  
		die( "<p>Unable to connect to the database server at this time.</p>" );
		} else { 
			if (!@mysql_select_db($this->dbase,$this->dbcon))  {
			die( "<p>Could not choose this database: ".$this->dbase." Please try again later.</p>" );
			}
		}
	} // connect end
	 

	function closeconnection() {
	  mysql_free_result();
	  mysql_close($this->dbcon);
	} // closeconnection end
	
}
?>


--- sql data object ---

<? 
require('path to the above mysql class file');

class sqlDataObject extends mysql {

	var $resultSet;
	var $update_string;

        /**
	* Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads.
	*
	*/
	function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') {
	
	if (!empty($where)) { $where = 'WHERE '.$where; }
	
	if (!empty($orderby)) { $orderby = 'ORDER BY '.$orderby; }
	
	if (!empty($limit)) { $limit = 'LIMIT '.$offset.','.$limit; }
	
	$query = mysql_query("SELECT $what_to_get FROM $table $where $orderby $sortorder $limit");
	if (!$query) {
		$this->errorHandler(mysql_error());
	}

	// Output as array
	if ($outputArray=='1') {
		while ($query_row = mysql_fetch_assoc($query)) {
			$this->resultSet[] = $query_row;
		}
	
	// Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result
	} else {
		$this->resultSet = $query;
	}
    }

     /* Handle potensially dangerous input */
	function safevalue ($value) {
		return mysql_real_escape_string(strip_slashes(trim($value)));
	}

}
?>

---- EXAMPLE USAGE ----

Let's say we want to output 

require('path-to-the-sql-data-object-file.php');

$sqlData = new sqlDataObject('example_database','username','password');




/* Get all news info where the id is less than 10 and ordr by the id. */

/* In this example let's say the database has only three columns: id, heading, message */

$some_news_output = $sqlData->getData('newstable','*','id<10','id');

while ($news_row = mysql_fetch_array($some_news_output)) {
   echo('<h1>'.$news_row['heading'].'</h1>'.
    $news_row['message'].'
   <p><a href="?page=news&amp;id="'.$news_row['id'].'">Read more about this</a></p>');
}

Revision: 15797
at July 16, 2009 20:10 by bakkelun


Updated Code
-- mysql connection handler --

<?

class mysql {
	var $dbase;
	var $dbcon;     
        var $db_user;
        var $db_pw;
	function connect($base,$user,$pw) {

	$this->dbase = $base;
	$this->db_user = $user;
	$this->db_pw = $pw;        

	$this->dbcon = mysql_connect("url-to-your-db",$this->db_user,$this->db_pw); 
	if (!$this->dbcon)  {  
		die( "<p>Unable to connect to the database server at this time.</p>" );
		} else { 
			if (!@mysql_select_db($this->dbase,$this->dbcon))  {
			die( "<p>Could not choose this database: ".$this->dbase." Please try again later.</p>" );
			}
		}
	} // connect end
	 

	function closeconnection() {
	  mysql_free_result();
	  mysql_close($this->dbcon);
	} // closeconnection end
	
}
?>


--- sql data object ---

<? 
require('path to the above mysql class file');

class sqlDataObject extends mysql {

	var $resultSet;
	var $update_string;

        /**
	* Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads.
	*
	*/
	function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') {
	
	if (!empty($where)) { $where = 'WHERE '.$where; }
	
	if (!empty($orderby)) { $orderby = 'ORDER BY '.$orderby; }
	
	if (!empty($limit)) { $limit = 'LIMIT '.$offset.','.$limit; }
	
	$query = mysql_query("SELECT $what_to_get FROM $table $where $orderby $sortorder $limit");
	if (!$query) {
		$this->errorHandler(mysql_error());
	}

	// Output as array
	if ($outputArray=='1') {
		while ($query_row = mysql_fetch_assoc($query)) {
			$this->resultSet[] = $query_row;
		}
	
	// Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result
	} else {
		$this->resultSet = $query;
	}
    }

     /* Handle potensially dangerous input */
	function safevalue ($value) {
		return mysql_real_escape_string(strip_slashes(trim($value)));
	}

}
?>

---- EXAMPLE USAGE ----

Let's say we want to output 

require('path-to-the-sql-data-object-file.php');

$sqlData = new sqlDataObject('example_database','username','password');




/* Get all news info where the id is less than 10 and ordr by the id. */

/* In this example let's say the database has only three columns: id, heading, message */

$some_news_output = $sqlData->getData('newstable','*','id<10','id');

while ($news_row = mysql_fetch_array($some_news_output)) {
   echo('<h1>'.$news_row['heading'].'</h1>'.
    $news_row['message'].'
   <p><a href="?page=news&amp;id="'.$news_row['id'].'">Read more about this</a></p>');
}

Revision: 15796
at July 16, 2009 20:09 by bakkelun


Initial Code
-- mysql connection handler --

<?

class mysql {
	var $dbase;
	var $dbcon;     
        var $db_user;
        var $db_pw;
	function connect($base,$user,$pw) {

	$this->dbase = $base;
	$this->db_user = $user;
	$this->db_pw = $pw;        

	$this->dbcon = mysql_connect("url-to-your-db",$this->db_user,$this->db_pw); 
	if (!$this->dbcon)  {  
		die( "<p>Unable to connect to the database server at this time.</p>" );
		} else { 
			if (!@mysql_select_db($this->dbase,$this->dbcon))  {
			die( "<p>Could not choose this database: ".$this->dbase." Please try again later.</p>" );
			}
		}
	} // connect end
	 

	function closeconnection() {
	  mysql_free_result();
	  mysql_close($this->dbcon);
	} // closeconnection end
	
}
?>


--- sql data object ---

require('path to the above mysql class file');

class sqlDataObject extends mysql {

	var $resultSet;
	var $update_string;

        /**
	* Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads.
	*
	*/
	function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') {
	
	if (!empty($where)) { $where = 'WHERE '.$where; }
	
	if (!empty($orderby)) { $orderby = 'ORDER BY '.$orderby; }
	
	if (!empty($limit)) { $limit = 'LIMIT '.$offset.','.$limit; }
	
	$query = mysql_query("SELECT $what_to_get FROM $table $where $orderby $sortorder $limit");
	if (!$query) {
		$this->errorHandler(mysql_error());
	}

	// Output as array
	if ($outputArray=='1') {
		while ($query_row = mysql_fetch_assoc($query)) {
			$this->resultSet[] = $query_row;
		}
	
	// Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result
	} else {
		$this->resultSet = $query;
	}
    }

     /* Handle potensially dangerous input */
	function safevalue ($value) {
		return mysql_real_escape_string(strip_slashes(trim($value)));
	}

}


---- EXAMPLE USAGE ----

Let's say we want to output 

require('path-to-the-sql-data-object-file.php');

$sqlData = new sqlDataObject('example_database','username','password');




/* Get all news info where the id is less than 10 and ordr by the id. */

/* In this example let's say the database has only three columns: id, heading, message */

$some_news_output = $sqlData->getData('newstable','*','id<10','id');

while ($news_row = mysql_fetch_array($some_news_output)) {
   echo('<h1>'.$news_row['heading'].'</h1>'.
    $news_row['message'].'
   <p><a href="?page=news&amp;id="'.$news_row['id'].'">Read more about this</a></p>');
}

Initial URL
http://www.dosspirit.net

Initial Description
Pretty straight forward implementation of mysql instantiation using php5 oop. Example usage at the bottom. The database in the example is named 'example_database' and has 1 table, 'newstable'. This table has three columns: 'id,heading,message'.

By bakkelun

Initial Title
sqlDataObj in PHP5

Initial Tags
sql

Initial Language
PHP