Location: PHPKode > scripts > Page nav class > page-nav-class/db_class.php
<?php
/**
*Class that could simplify mysql database operations.Used in attached page-nav class
*
*
Sample

	$dbDetails['DB_USER'] = username;
	$dbDetails['DB_PASS'] = password;
	$dbDetails['DB_SERVER'] = server;
	$dbDetails['DB_NAME'] = database;
	$db = new MySQL($dbDetails);	
	$db->connectdb();
	//select query usage
	$sql = "SELECT * FROM updates  ";
	$latest_updates = $db->select_sql($sql);
	$row_latest_updates = $latest_updates[0];
	$totalRows_latest_updates = count($latest_updates);
	
	//other querie usage
	$SQL="delete fromcurrent_users where  DATE_ADD(last_visited,INTERVAL 1 year)<NOW()";
	$db->execute_sql($SQL);

*
* @package database and pagenav class
* @author Sreekanth Dayanand <hide@address.com>
* @version 1.0 <2009/09/18>
* @copyright GNU General Public License (GPL)
**/
class MySQL {
	var $user;
	var $pass;
	var $server;
	var $db;
	var $con;
	var $query;
	
	//--------------------------------------------------------
	function MySQL($dbDetails)
	//--------------------------------------------------------
	{
		$this->user = $dbDetails['DB_USER'];
		$this->pass = $dbDetails['DB_PASS'];
		$this->server = $dbDetails['DB_SERVER'];
		$this->db = $dbDetails['DB_NAME'];
	}
	
	//--------------------------------------------------------
	function connectdb()
	//--------------------------------------------------------
	{
		$this->con = mysql_pconnect($this->server, $this->user, $this->pass) or die("Failed to connect to database".$this->db);
		mysql_select_db($this->db) or die ("Failed to select a database");
	}
	function getVersion()
	{
		return mysql_get_server_info();
	}
	
	//--------------------------------------------------------
	function setquery($sql) 
	//--------------------------------------------------------
	{ global $site_options;
		$this->query=$sql;
		if($site_options[debug_mode]) echo $sql."<br />";
	}
	
	//--------------------------------------------------------
	function disconnect()
	//--------------------------------------------------------
	{
		mysql_close($this->con);
	}
	/**
    *returns the resultset as an array
    *@access public
    *@param integer height
    *@return boolean
    **/
	//----------------------------------------------------------------------------
	function select()
	//----------------------------------------------------------------------------
	{
	    $result = mysql_query($this->query) or die( $this->query."<hr>".mysql_error() );
		$ret=array();
	    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	     	$ret[]=$line;
	    }
	    mysql_free_result($result);
		return $ret;
	}
		
	//For insert, update, delete
	//--------------------------------------------------------
	function sql()
	//--------------------------------------------------------
	{
		mysql_query($this->query) or die( $this->query."<hr>".mysql_error() );
	}
	
	//For insert, update, delete
	//--------------------------------------------------------
	function sqlnodie()
	//--------------------------------------------------------
	{
		mysql_query($this->query);
	}
	
	function select_sql($sql)
	{
	 $this->setquery($sql);
	 return $this->select();
	}
	function execute_sql($sql)
	{
	 $this->setquery($sql);
	 $this->sql();
	}
	
}
?>
Return current item: Page nav class