Mysql database operation class of version 1127 available for download

  • 2020-03-31 21:17:35
  • OfStack

Mysql.class.php (link: http://xiazai.jb51.net/201012/yuanma/Mysql_class.rar)
 
<?php 
class Mysql { 
private $db_host; //The host address
private $db_user; //The user name
private $db_pass; //Connect the password
private $db_name; //The name of the
private $db_charset; //coding
private $conn; 
public $debug=false;//Debug switch, off by default
private $query_id; //To determine if the SQL statement was executed successfully
private $result; //The result set
private $num_rows; //Number of rows in the result set, valid for select only
private $insert_id; //The ID generated by the previous INSERT operation
//Construct/destructor
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) { 
$this->db_host = $db_host ; 
$this->db_user = $db_user ; 
$this->db_pass = $db_pass ; 
$this->db_name = $db_name ; 
$this->db_charset = $db_charset ; 
$this->conn = $conn ; 
$this->connect(); 
} 
function __destruct () { 
@mysql_close($this->conn); 
} 
//Connect/select database
public function connect () { 
if ($this->conn == 'pconn') { 
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass); 
} else { 
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass); 
} 
if (!$this->conn) { 
$this->show_error(' The database - Connection failed: username or password error! '); 
} 
if (!@mysql_select_db($this->db_name,$this->conn)) { 
$this->show_error(" The database - Selection failed: database  $this->db_name  Do not use "); 
} 
mysql_query("SET NAMES $this->db_charset"); 
return $this->conn; 
} 
//The query method
public function query ($sql) { 
if ($this->query_id) $this->free_result(); 
$this->query_id = @mysql_query($sql,$this->conn); 
if (!$this->query_id) $this->show_error("SQL statements  <b>"$sql"</b>  An error was encountered during execution "); 
return $this->query_id; 
} 
//Displays detailed error messages
public function show_error ($msg) { 
if($this->debug){ 
$errinfo = mysql_error(); 
echo " Error: $msg <br/>  Returns: $errinfo<p>"; 
}else{ 
echo '<p> Error! <p>'; 
} 
} 
//Gets information on the success or failure of query execution
public function get_query_info($info){ 
if ($this->query_id) { 
echo $info; 
} 
} 
//Query all
public function findall ($table_name) { 
$this->query("select * from $table_name"); 
} 
// mysql_fetch_array 
public function fetch_array () { 
if ($this->query_id) { 
$this->result = mysql_fetch_array($this->query_id); 
return $this->result; 
} 
} 
// ...... 
public function fetch_assoc () { 
if ($this->query_id) { 
$this->result = mysql_fetch_assoc($this->query_id); 
return $this->result; 
} 
} 
public function fetch_row () { 
if ($this->query_id) { 
$this->result = mysql_fetch_row($this->query_id); 
return $this->result; 
} 
} 
public function fetch_object () { 
if ($this->query_id) { 
$this->result = mysql_fetch_object($this->query_id); 
return $this->result; 
} 
} 
//Access to num_rows
public function num_rows () { 
if ($this->query_id) { 
$this->num_rows = mysql_num_rows($this->query_id); 
return $this->num_rows; 
} 
} 
//Get insert_id
public function insert_id () { 
return $this->insert_id = mysql_insert_id(); 
} 
//Shows how many tables there are
public function show_tables () { 
$this->query("show tables"); 
if ($this->query_id) { 
echo " The database  $this->db_name  A total of  ".$this->num_rows($this->query_id)."  table <br/>"; 
$i = 1; 
while ($row = $this->fetch_array($this->query_id)){ 
echo "$i -- $row[0]<br/>"; 
$i ++; 
} 
} 
} 
//Shows how many databases there are
public function show_dbs(){ 
$this->query("show databases"); 
if ($this->query_id) { 
echo " Common database  ".$this->num_rows($this->query_id)."  a <br/>"; 
$i = 1; 
while ($this->row = $this->fetch_array($this->query_id)){ 
echo "$i -- ".$this->row[Database]."<br />"; 
$i ++; 
} 
} 
} 
//Delete database: returns the delete result
public function drop_db ($db_name='') { 
if ($db_name == '') { 
$db_name = $this->db_name;//The current database is deleted by default
$this->query("DROP DATABASE $db_name"); 
}else { 
$this->query("DROP DATABASE $db_name"); 
} 
if ($this->query_id) { 
return " The database  $db_name  Delete the success "; 
}else { 
$this->show_error(" The database  $db_name  Delete failed "); 
} 
} 
//Delete table: returns the delete result
public function drop_table ($table_name) { 
$this->query("DROP TABLE $table_name"); 
if ($this->query_id) { 
return " The data table  $table_name  Delete the success "; 
}else { 
$this->show_error(" The data table  $table_name  Delete failed "); 
} 
} 
//Create a database
public function create_db ($db_name) { 
$this->query("CREATE DATABASE $db_name"); 
if($this->query_id){ 
return " The database  $db_name  Creating a successful "; 
}else { 
$this->show_error(" The database  $db_name  Create a failure "); 
} 
} 
//Get the database version
public function get_info(){ 
echo mysql_get_server_info(); 
} 
//Free memory
public function free_result () { 
if ( @mysql_free_result($this->query_id) ) 
unset ($this->result); 
$this->query_id = 0; 
} 
} // End class 
?> 


Related articles: