Location: PHPKode > projects > Replex > replex-0.9.0/db/class_mysql.php
<?php
/*
$Id: class_mysql.php,v 1.3 2001/03/24 23:30:00 edink Exp $
This is a PHP class definition for mysql database server connections.
Last Modified on 2/9/2001 1:59PM Justin Koivisto [Koivi Media]


24/03/2001:
Class modified by Jimi Rønberg.
Most functions now return either 1 or 0.
*/


class mysql_class{
	# Constructor
	function mysql_class($user,$pass,$host){
		$this->user=$user;
		$this->pass=$pass;
		$this->host=$host;
		$this->id=@mysql_pconnect($this->host,$this->user,$this->pass) or
			mysql_ErrorMsg("Unable to connect to mysql server: $this->host");
	}

	# Get a list of the available databases on the current server
	function GetDatabases(){
		$this->result=@mysql_list_dbs() or
			mysql_ErrorMsg("Unable to find a database on server: $this->host");
		$i=0;
		while($i < mysql_num_rows($this->result)){
			$db_names[$i]=mysql_tablename($this->result,$i);
			$i++;
		}
		return($db_names);
	}

	# Create a database on the server
	function CreateDB($database){
		$this->result= @mysql_create_db($database) or
			mysql_ErrorMsg("Unable to create database: $database");
		$this->a_rows=@mysql_affected_rows($this->result);
	}

	# Drop a database
	function DropDB($database){
		$this->result=@mysql_drop_db($database) or
			mysql_ErrorMsg("Unable to drop database: $database");
		$this->a_rows=@mysql_affected_rows($this->result);
	}

	# Copy a database from this server (even to another)
	# Returns TRUE or FALSE for success or failure
	function CopyDB($database,$dest_db='',$dest_host="localhost",$dest_user="phpuser",$dest_pass="php"){
		# Until I get time to figure this out with using the php
		# functions for mysql, you'll need to know these for the system
		# 1/28/2001 6:54PM
		$MYSQLPATH="/usr/local/bin/";
		$MYSQLDUMP= $MYSQLPATH . "mysqldump";
		$MYSQL= $MYSQLPATH . "mysql";

		if($dest_db='') $dest_db=$database;
		$db_checker=SelectDB($database);
		if(!$db_checker)
			mysql_ErrorMsg("Database $database does not exist.");

		$dest = new MySQL_class($dest_user, $dest_pass, $dest_host);
		$db_checker = $dest->SelectDB($dest_db);
		if(!$db_checker)
			$dest->Create_DB($dest_db);

		# Now that connection has been established, we can do the copying
		$system_command= "$MYSQLDUMP -u$this->user -p$this->pass --opt $DATABASE | $MYSQL --host=$dest_host -u$dest_user -p$dest_pass -C $dest_db";
		system($system_command,$system_result);
		if($system_result)
			return FALSE;
		else
			return TRUE;
	}

	# Select or change databases
	function SelectDB($db){
		$this->db=$db;
		@mysql_select_db($db,$this->id) or
			mysql_ErrorMsg("Unable to select database: $db");
	}

	# Get a list of the available tables in this database
	function GetTableList(){
		$this->result=@mysql_list_tables($this->db,$this->id) or
			mysql_ErrorMsg("Unable to find any tables in database: $this->db");
		$i=0;
		while($i < mysql_num_rows($this->result)){
			$tb_names[$i]=mysql_tablename($this->result,$i);
			$i++;
		}
		return($tb_names);
	}

	# Get a list of the fields names in the given table of the current database
	function GetFieldList($tbl_name){
		$this->result=@mysql_list_fields($this->db,$tbl_name,$this->id);
		$i=0;
		while($i < mysql_num_fields($this->result)){
			$fd_names[$i]=mysql_field_name($this->result,$i);
			$i++;
		}
		return($fd_names);
	}

	# Delete rows from a table
	function Delete($query){
		$this->result=@mysql_query($query,$this->id);
		$this->a_rows=@mysql_affected_rows($this->result);
		if($this->result) return 1;
		else return 0;
	}

	# Update elements in database
	function Update($query){
		$this->result=@mysql_query($query,$this->id);			
		$this->a_rows=@mysql_affected_rows($this->result);
		if($this->result) return 1;
		else return 0;
	}

	# Insert row into a table
	function Insert($query){
		$this->result=@mysql_query($query,$this->id);
		$this->a_rows=@mysql_affected_rows($this->result);
		if($this->result) return 1;
		else return 0;
	}

	# Get last insert id from an auto_incremented field
	function InsertID(){
		$this->result=@mysql_insert_id($this->id) or
			mysql_ErrorMsg("Cannot retrieve auto_increment value: $this->id");
		return($this->result);
	}

	# Multiple row return query - Use GetRow function to loop through
	function Query($query){
		$this->result=@mysql_query($query,$this->id);
		$this->rows=@mysql_num_rows($this->result);
		if($this->result) return 1;
		else return 0;
	}

	# Get a row of data from a multiple row query
	function GetRow($row){
		@mysql_data_seek($this->result,$row);
		$this->data=@mysql_fetch_array($this->result);
	}

	# Single row return query
	function QueryRow($query){
		$this->result=@mysql_query($query,$this->id);
		$this->rows=@mysql_num_rows($this->result);
		$this->data=@mysql_fetch_array($this->result);
		return($this->data);
	}

	# Single element return query
	function QueryItem($query){
		$this->result=@mysql_query($query,$this->id);
		$this->rows=@mysql_num_rows($this->result);
		$this->data=@mysql_fetch_array($this->result);
		return($this->data[0]);
	}

	# Use if checking for empty query result returns 0 if empty,
	# and 1 if there is at least one result
	function Exists($query){
		$this->result=@mysql_query($query,$this->id);
		if(@mysql_num_rows($this->result)) return 1;
		else return 0;
	}
} # End of class

# MySQL error message function
function mysql_ErrorMsg($msg){
	# Get out of html constraints so we can see the message
	echo("</ul></ul></ul></dl></dl></dl></ol></ol></ol>\n");
	echo("</table></table></table></script></script></script>\n");

	# Display the error message
	$text ="<font color=\"#ff0000\" size=+2><p>Error: $msg :";
	$text .= mysql_error();
	$text .= "</font>\n";
	die($text);
}

$DBCONN = new mysql_class($MYSQL_USERNAME,$MYSQL_PASSWORD,$MYSQL_HOST);
$DBCONN->SelectDB($MYSQL_DB);
?>
Return current item: Replex