Location: PHPKode > scripts > classMySQL > classmysql/classMySQL.php
<?  
	class db_layout {  
		var $classname="db_layout";  
  
		var $db_name;  
		var $db_user;  
		var $db_password;  
		var $db_host;  
		var $db_link_ptr;  
		  
		var $tables;  
		var $fields;  
		  
	}  
  
	class db_mysql extends db_layout {  
		var $classname="db_mysql";  
  
		var $db_result;  
		var $db_affected_rows;  
  
		var $saved_results=array();  
		var $results_saved=0;  
		  
		function error($where="",$error,$errno) {  
			echo "$where<br>";  
			die($error."<br>".$errno);  
		}  
				  
		function error_msg() {  
			return mysql_error();  
		}  
		  
		function PushResults() {  
			$this->saved_results[$this->results_saved]=array($this->db_result,$this->db_affected_rows);  
			$this->results_saved++;  
		}  
		  
		function PopResults() {  
			$this->results_saved--;  
			$this->db_result=$this->saved_results[$this->results_saved][0];  
			$this->db_affected_rows=$this->saved_results[$this->results_saved][1];  
		}  
		  
		function db_mysql($host, $user, $passwd, $db, $create="") {  
			$this->db_name=$db;  
			$this->db_user=$user;  
			$this->db_passwd=$passwd;  
			$this->db_host=$host;  
			  
			$this->db_link_ptr=@mysql_connect($host,$user,$passwd) or $this->error("",mysql_error(),mysql_errno());  
			$this->dbhandler=@mysql_select_db($db);  
			if (!$this->dbhandler) {  
				if ($create=="1")  {  
					@mysql_create_db($db,$this->db_link_ptr) or $this->error("imposible crear la base de datos.",mysql_error(),mysql_errno());;  
					$this->dbhandler=@mysql_select_db($db);  
				}  
			}  
		}  
  
		function reselect_db($db){  
			$this->dbhandler=@mysql_select_db($db);  
		}  
		  
		function closeDB() {  
			@mysql_close($this->db_link_ptr);  
		}  
		  
		function create_table($tblName,$tblStruct) {  
			if (is_array($tblStruct)) $theStruct=implode(",",$tblStruct); else $theStruct=$tblStruct;  
			@mysql_query("create table $tblName ($theStruct)") or $this->error("create table $tblName ($theStruct)",mysql_error(),mysql_errno());  
		}  
		  
		function drop_table($tblName) {  
			@mysql_query("drop table if exists $tblName") or $this->error("drop table $tblName",mysql_error(),mysql_errno());  
		}  
		  
		function raw_query($sql_stat) {  
			$this->db_result=@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno());  
			$this->db_affected_rows=@mysql_num_rows($this->db_result);  
		}  
  
		function count_records($table,$filter="") {  
			$this->db_result=@mysql_query("select count(*) as num from $table".(($filter!="")?" where $filter" : ""));  
			$xx=@mysql_result($this->db_result,0,"num");  
			return $xx;  
		}  
				  
		function select($fields,$tables,$where="",$order_by="",$group_by="",$having="",$limit="") {  
			  
			$sql_stat=" select $fields from $tables ";  
			  
			if (!empty($where)) $sql_stat.="where $where ";  
			if (!empty($group_by)) $sql_stat.="group by $group_by ";  
			if (!empty($order_by)) $sql_stat.="order by $order_by ";  
			if (!empty($having)) $sql_stat.="having $having ";  
			if (!empty($limit)) $sql_stat.="limit $limit ";  
			  
			$this->db_result=@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno());  
			$this->db_affected_rows=@mysql_num_rows($this->db_result);  
			  
			return $sql_stat;  
		}  
		  
		function list_tables() {  
			$this->db_result=@mysql_list_tables($this->db_name);  
			$this->db_affected_rows=@mysql_num_rows($this->db_result);  
			return $this->db_result;  
		}  
		
                function describe($tablename) {
                        $this->result=@mysql_query("describe $tablename");
                }

                function table_exists($tablename) {
                        $this->pushresults();
                        $description=$this->describe($tablename);
                        $this->popresults();
                        if ($description) $exists=true; else $exists=false;
                        return $exists;
                }

		function tablename($tables, $tbl) {  
			return mysql_tablename($tables,$tbl);  
		}  
  
		function insert_id() {  
			return mysql_insert_id();  
		}  
				  
		function insert($table,$fields="",$values="") {  
			$sql_stat="insert into $table ";  
			  
			if (is_array($fields)) $theFields=implode(",",$fields); else $theFields=$fields;  
			if (is_array($values)) $theValues="'".implode("','",$values)."'"; else $theValues=$values;  
			  
			$theValues=str_replace("'now()'","now()",$theValues);  
			  
			if (!empty($theFields)) $sql_stat.="($theFields) ";  
			$sql_stat.="values ($theValues)";  
			  
			@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno());  
		}  
  
		function update($table,$newvals,$where="") {  
			if (is_array($newvals)) $theValues=implode(",",$newvals); else $theValues=$newvals;  
			  
			$sql_stat="update $table set $theValues";  
			  
			if (!empty($where)) $sql_stat.=" where $where";  
			@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno());  
		}  
		  
		function delete($table,$where="") {  
			  
			$sql_stat="delete from $table ";  
			  
			if (!empty($where)) $sql_stat.="where $where ";  
			  
			$db_result2=@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno());  
			$this->db_affected_rows=@mysql_affected_rows($this->db_result2);  
		}  
		  
		function free() {  
			@mysql_free_result($this->db_result) or $this->error("",mysql_error(),mysql_errno());  
		}  
		  
		function fetch_row() {  
			$row=mysql_fetch_row($this->db_result);  
			return $row;  
		}  
  
		function result($recno,$field) {  
			return mysql_result($this->db_result,$recno,$field);  
		}  
  
		function num_fields(){  
			return mysql_num_fields($this->db_result);  
		}  
		  
		function fetch_array() {  
			$row=mysql_fetch_array($this->db_result);  
			return $row;  
		}  
  
		function fetch_field() {  
			$row=mysql_fetch_field($this->db_result);  
			return $row;  
		}  
	}  
?>
Return current item: classMySQL