user=$user; $this->pass=$pass; $this->host=$host; $this->port=$port; $this->socket=$socket_path; if($this->port) $host.=":".$this->port; if($this->socket) $host.=":".$this->socket; $this->id=@mysql_connect($host,$this->user,$this->pass) or mysql_ErrorMsg("Unable to connect to mysql server: $this->host"); } 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); } 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); } 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); } function CopyDB($database,$dest_db='',$drop_tables=0,$dest_host="localhost",$dest_user="phpuser", $dest_pass="php",$dest_port='',$dest_socket_path=''){ set_time_limit(600); if($dest_port) $host.=":".$dest_port; if($dest_socket_path) $host.=":".$dest_socket_path; $conn_id=@mysql_connect($host,$dest_user,$dest_pass) or mysql_ErrorMsg("Unable to connect to mysql server: $dest_host"); $dest_dbs=@mysql_list_dbs($conn_id) or mysql_ErrorMsg("Unable to find a database on server: $dest_host"); $EXISTS=FALSE; while($iid); $num_tables=@mysql_num_rows($tables) or mysql_ErrorMsg("No tables found on database: $database, host: $this->host"); for($i=0;$i<$num_tables;$i++){ set_time_limit(60); $table=mysql_tablename($tables,$i); "$dest_user,$dest_pass,$dest_port,$dest_socket_path);
"; $this->CopyTable($table,$table,$database,$dest_db,$drop_tables,$conn_id,$dest_host, $dest_user,$dest_pass,$dest_port,$dest_socket_path); } return TRUE; } function CopyTable($table,$dest_table,$database,$dest_db,$drop_table='',$conn_id='',$dest_host='', $dest_user='',$dest_pass='',$dest_port='',$dest_socket_path=''){ if(empty($conn_id)){ $host=$dest_host; if($dest_port) $host.=":".$dest_port; if($dest_socket_path) $host.=":".$dest_socket_path; $conn_id=@mysql_connect($dest_host,$dest_user,$dest_pass) or mysql_ErrorMsg("Unable to connect to mysql server: $dest_host"); } if($drop_table){ $drop="DROP TABLE IF EXISTS $dest_table;"; $result=@mysql_db_query($dest_db,$drop,$conn_id) or mysql_ErrorMsg("Unable to perform query (drop $table) on database: $dest_host:". "$dest_db

$drop

"); } $struc="CREATE TABLE $dest_table (\n"; $result=@mysql_db_query($database,"SHOW FIELDS FROM $table",$this->id) or mysql_ErrorMsg("Unable to copy table (fields): $table from database $database"); while($row=mysql_fetch_array($result)){ $struc.=" $row[Field] $row[Type]"; if(isset($row['Default']) && (!empty($row['Default']) || $row['Default']=="0")) $struc.=" DEFAULT '$row[Default]'"; if($row['Null']!="YES") $struc.=" NOT NULL"; if($row['Extra']!="") $struc.=" $row[Extra]"; $struc.=",\n"; } $struc=ereg_replace(",\n$","",$struc); $result=mysql_db_query($database,"SHOW KEYS FROM $table",$this->id) or mysql_ErrorMsg("Unable to copy table (keys): $table from database $database"); while($row=mysql_fetch_array($result)){ $key_name=$row['Key_name']; if(($key_name!='PRIMARY') && ($row['Non_unique']==0)) $key_name="UNIQUE|$key_name"; if(!isset($index[$key_name])) $index[$key_name]=array(); $index[$key_name][]=$row['Column_name']; } while(list($x,$columns)=@each($index)){ $struc.=",\n"; if($x=="PRIMARY") $struc.=" PRIMARY KEY (".implode($columns,", ").")"; else if(substr($x,0,6)=="UNIQUE") $struc.=" UNIQUE ".substr($x,7)." (".implode($columns, ", ").")"; else $struc.=" KEY $x (".implode($columns, ", ").")"; } $struc.="\n);"; $struc=stripslashes(trim($struc)); $result=@mysql_db_query($dest_db,$struc,$conn_id) or mysql_ErrorMsg("Unable to perform query (structure $table) on database: $dest_host: $dest_db"); $result=@mysql_db_query($database,"SELECT * FROM $table",$this->id) or mysql_ErrorMsg("Unable to perform query: $query"); $num_rows=@mysql_num_rows($result); $insert_data=array(); for($i=0;$i<$num_rows;$i++){ @mysql_data_seek($result,$i) or mysql_ErrorMsg("Unable to seek data row: $row"); $data=@mysql_fetch_array($result) or mysql_ErrorMsg("Unable to fetch row: $row"); $field_list="("; $values="("; while(list($key,$val)=@each($data)){ if(!is_int($key)){ $field_list.="$key, "; $values.="'".addslashes($val)."', "; } } $field_list=ereg_replace(", $",")",$field_list); $values=ereg_replace(", $",")",$values); $insert_data[]="INSERT INTO $table $field_list VALUES $values"; } foreach($insert_data as $query){ $result=@mysql_db_query($dest_db,$query,$conn_id) or mysql_ErrorMsg("Unable to perform query (data $table) on database: $dest_host: $dest_db"); } return TRUE; } function SelectDB($db){ $this->db=$db; @mysql_select_db($db,$this->id) or mysql_ErrorMsg("Unable to select database: $db"); } 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); } 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); } function Delete($query){ $this->result=@mysql_query($query,$this->id) or mysql_ErrorMsg("Unable to perform Delete: $query"); $this->a_rows=@mysql_affected_rows($this->result); } function Update($query){ $this->result=@mysql_query($query,$this->id) or mysql_ErrorMsg("Unable to perform update: $query"); $this->a_rows=@mysql_affected_rows($this->result); } function Insert($query){ $this->result=@mysql_query($query,$this->id) or mysql_ErrorMsg("Unable to perform insert: $query"); $this->a_rows=@mysql_affected_rows($this->result); } function InsertID(){ $this->result=@mysql_insert_id($this->id) or mysql_ErrorMsg("Cannot retrieve auto_increment value: $this->id"); return($this->result); } function Query($query){ $this->result=@mysql_query($query,$this->id) or mysql_ErrorMsg("Unable to perform query: $query"); $this->rows=@mysql_num_rows($this->result); } function GetRow($row){ @mysql_data_seek($this->result,$row) or mysql_ErrorMsg("Unable to seek data row: $row"); $this->data=@mysql_fetch_array($this->result) or mysql_ErrorMsg("Unable to fetch row: $row"); } function QueryRow($query){ $this->result=@mysql_query($query,$this->id) or mysql_ErrorMsg("Unable to perform query: $query"); $this->rows=@mysql_num_rows($this->result); $this->data=@mysql_fetch_array($this->result) or mysql_ErrorMsg("Unable to fetch data from query: $query"); return($this->data); } function QueryItem($query){ $this->result=@mysql_query($query,$this->id) or mysql_ErrorMsg("Unable to perform query: $query"); $this->rows=@mysql_num_rows($this->result); $this->data=@mysql_fetch_array($this->result) or mysql_ErrorMsg("Unable to fetch data from query: $query"); return($this->data[0]); } function Exists($query){ $this->result=@mysql_query($query,$this->id); if(@mysql_num_rows($this->result)) return TRUE; else return FALSE; } } function mysql_ErrorMsg($msg){ echo("\n"); echo("\n"); $text ="

Error: $msg :"; $text .= mysql_error(); $text .= "\n"; die($text); } } ?>