An in depth understanding of php's MySQL join class

  • 2020-06-07 04:09:14
  • OfStack

Accidentally found in the computer there is such a Mysql connection class, also do not remember where the collection, paste it up.
The next few show_databases and show_tables... 1 heap echo methods are used, like 1 straight don't like directly use output statements in a class method, but this is only list database and table names, the parameters of the constructor can also give a default value.

<?php
/*
* filename : mysql Database connection class 
*/
class mysql{
 private $db_host;  // Database host 
 private $db_user;  // Database user name 
 private $db_pwd;   // Database username and password 
 private $db_database;    // The database name 
 private $conn;           // Database connection id ;
 private $result;         // perform query The resulting resource identity for the command 
 private $sql;   //sql Execute the statement 
 private $row;     // The number of entries returned 
 private $coding;  // Database coding, GBK,UTF8,gb2312
 private $bulletin = true;    // Whether error logging is enabled 
 private $show_error = true;  // Test phase, display all errors , Have a safety hazard , Off by default 
 private $is_error = false;   // Whether an error is found and terminated immediately , The default true, It is recommended not to enable because it is annoying for users to see nothing when there is a problem 
 /* The constructor */
 public function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$coding){
      $this->db_host=$db_host;
      $this->db_user=$db_user;
      $this->db_pwd = $db_pwd;
      $this->db_database=$db_database;
      $this->conn=$conn;
      $this->coding=$coding;
      $this->connect();
    }
 /* Database connection */
 public function connect()
 {
  if($this->conn=="pconn"){
   // Permanent link 
      $this->conn=mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
  }else{
   // Immediate access 
   $this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
  }
  if(!mysql_select_db($this->db_database,$this->conn)){
   if($this->show_error){
    $this->show_error(" Database unavailable: ",$this->db_database);
   }
  }
  mysql_query("SET NAMES $this->coding");
 }
 /* The database executes statements that can execute queries to add modifications to remove anything sql statements */
 public function query($sql)
 {
  if($sql == ""){
  $this->show_error("sql Statement error: ","sql The query statement is empty ");}
     $this->sql = $sql;
     $result = mysql_query($this->sql,$this->conn);
  if(!$result){
   // In debugging, sql Statement errors are automatically printed 
   if($this->show_error){
    $this->show_error(" error sql Statement: ",$this->sql);
   }
  }else{
   $this->result = $result;
  }
     return $this->result;
 }
 /* Create and add a new database */
 public function create_database($database_name){
  $database=$database_name;
  $sqlDatabase = 'create database '.$database;
  $this->query($sqlDatabase);
 }
 /* Query the server for all databases */
 // Separate the system database from the user database for a more intuitive display? 
 public function show_databases(){
  $rs=$this->query("show databases");
  echo " Existing database: ".$amount =$this->db_num_rows($rs);
  echo "<br />";
  $i=1;
  while($row = $this->fetch_array($rs)){
   echo "$i $row[Database]";
   echo "<br />";
   $i++;
  }
 }
 // Returns an array of all database names on the host 
 public function databases()
 {
  $rsPtr=mysql_list_dbs($this->conn);
  $i=0;
  $cnt=mysql_num_rows($rsPtr);
  while($i<$cnt)
  {
    $rs[]=mysql_db_name($rsPtr,$i);
    $i++;
  }
  return $rs;
 }
 /* Query all the tables under the database */
 function show_tables($database_name){
  $this->query("show tables");
  echo " Existing database: ".$amount = $this->db_num_rows($rs);
  echo "<br />";
  $i=1;
  while($row = $this->fetch_array($rs)){
   $columnName="Tables_in_".$database_name;
   echo "$i $row[$columnName]";
   echo "<br />";
   $i++;
  }
 }
 /*
 mysql_fetch_row()    array  $row[0],$row[1],$row[2]
 mysql_fetch_array()  array  $row[0]  or  $row[id]
 mysql_fetch_assoc()  array   with $row->content  Field case sensitive 
 mysql_fetch_object() object  with $row[id],$row[content]  Field case sensitive 
 */
 /* Result data */
 public function mysql_result_li()
 {
  return mysql_result($str);
 }
 /* Acquisition of record set , Access to an array - Indexes and associations , use $row['content'] */
 public function fetch_array()
 {
  return mysql_fetch_array($this->result);
 }
 // Get associative array , use $row[' The field name ']
 public function fetch_assoc()
 {
  return mysql_fetch_assoc($this->result);
 }
 // Gets an array of numeric indexes , use $row[0],$row[1],$row[2]
 public function fetch_row()
 {
  return mysql_fetch_row($this->result);
 }
 // Get an array of objects , use $row->content
 public function fetch_Object()
 {
  return mysql_fetch_object($this->result);
 }
 // To simplify the query select
 public function findall($table)
 {
  $this->query("SELECT * FROM $table");
 }
 // To simplify the query select
 public function select($table,$columnName,$condition)
 {
  if($columnName==""){
   $columnName="*";
  }
  $this->query("SELECT $columnName FROM $table $condition");
 }
 // Simplified deleted del
 public function delete($table,$condition){
  $this->query("DELETE FROM $table WHERE $condition");
 }
 // Simplify the insert insert
 public function insert($table,$columnName,$value){
  $this->query("INSERT INTO $table ($columnName) VALUES ($value)");
 }
 // Simplify the modification update
 public function update($table,$mod_content,$condition){
  $this->query("UPDATE $table SET $mod_content WHERE $condition");
 }
 /* Get on 1 step  INSERT  Operationally generated  ID*/
 public function insert_id(){
  return mysql_insert_id();
    }
 // Directed definite 1 Bar data record 
 public function db_data_seek($id){
  if($id>0){
   $id=$id-1;
  }
  if(!@mysql_data_seek($this->result,$id)){
   $this->show_error("sql Incorrect statement: ", " The specified data is empty ");
  }
  return $this->result;
 }
 //  According to the select Query results calculate the number of result sets 
 public function db_num_rows(){
   if($this->result==null){
    if($this->show_error){
     $this->show_error("sql Statement is wrong "," Temporarily empty, no content! ");
   }
   }else{
    return  mysql_num_rows($this->result);
   }
 }
 //  According to the insert,update,delete The execution result gets the number of affected rows 
 public function db_affected_rows(){
   return mysql_affected_rows();
 }
 // The output shows sql statements 
 public function show_error($message="",$sql=""){
  if(!$sql){
   echo "<font color='red'>".$message."</font>";
   echo "<br />";
  }else{
   echo "<fieldset>";
   echo "<legend> Error message prompt :</legend><br />";
   echo "<div style="font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;" mce_style="font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;">";
   echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";
   echo "<font color='white'> Error no. : 12142</font>";
   echo "</div><br />";
   echo " Reason for error: ".mysql_error()."<br /><br />";
   echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";
   echo "<font color='white'>".$message."</font>";
   echo "</div>";
   echo "<font color='red'><pre>".$sql."</pre></font>";
    $ip=$this->getip();
    if($this->bulletin){
     $time = date("Y-m-d H:i:s");
     $message=$message."/r/n$this->sql"."/r/n The customer IP:$ip"."/r/n time  :$time"."/r/n/r/n";
     $server_date=date("Y-m-d");
     $filename=$server_date.".txt";
     $file_path="error/".$filename;
     $error_content=$message;
     //$error_content=" The wrong database cannot be linked ";
     $file = "error"; // Set the file save directory 
     // Create a folder 
     if(!file_exists($file)){
      if(!mkdir($file,0777)){
      // The default  mode  is  0777 , which means the most possible access 
       die("upload files directory does not exist and creation failed");
      }
     }
     // To establish txt The date file 
     if(!file_exists($file_path)){
      //echo " Establishment date file ";
      fopen($file_path,"w+");
      // First make sure the file exists and is writable 
      if (is_writable($file_path))
      {
       // Open in add mode $filename The file pointer will be at the beginning of the file 
       if (!$handle = fopen($file_path, 'a'))
       {
        echo " Can't open a file  $filename";
        exit;
       }
        // will $somecontent Write to the file we opened. 
       if (!fwrite($handle, $error_content))
       {
        echo " Could not write to file  $filename";
        exit;
       }
       //echo " file  $filename  Write to successful ";
       echo " -- The error record is saved !";
       // Close the file 
       fclose($handle);
      } else {
       echo " file  $filename  Do not write ";
      }
     }else{
      // First make sure the file exists and is writable 
      if (is_writable($file_path))
      {
       // Open in add mode $filename The file pointer will be at the beginning of the file 
       if (!$handle = fopen($file_path, 'a'))
       {
        echo " Can't open a file  $filename";
        exit;
       }
        // will $somecontent Write to the file we opened. 
       if (!fwrite($handle, $error_content))
       {
        echo " Could not write to file  $filename";
        exit;
       }
       //echo " file  $filename  Write to successful ";
       echo " -- The error record is saved !";
       // Close the file 
       fclose($handle);
      } else {
       echo " file  $filename  Do not write ";
      }
     }
    }
    echo "<br />";
    if($this->is_error){
     exit;
    }
   }
   echo "</div>";
   echo "</fieldset>";
  echo "<br />";
 }
 // Release result set 
 public function free(){
  @mysql_free_result($this->result);
 }
 // Database selection 
 public function select_db($db_database){
  return mysql_select_db($db_database);
 }
 // Number of query fields 
 public function num_fields($table_name){
  //return mysql_num_fields($this->result);
  $this->query("select * from $table_name");
  echo "<br />";
  echo " Number of fields: ".$total = mysql_num_fields($this->result);
  echo "<pre>";
  for ($i=0; $i<$total; $i++){
   print_r(mysql_fetch_field($this->result,$i) );
  }
  echo "</pre>";
  echo "<br />";
 }
 // achieve  MySQL  Server information 
 public function mysql_server($num=''){
  switch ($num){
   case 1 :
   return mysql_get_server_info(); //MySQL  Server information 
   break;
   case 2 :
   return mysql_get_host_info();   // achieve  MySQL  Host information 
   break;
   case 3 :
   return mysql_get_client_info(); // achieve  MySQL  Client information 
   break;
   case 4 :
   return mysql_get_proto_info();  // achieve  MySQL  Protocol information 
   break;
   default:
   return mysql_get_client_info(); // The default in mysql Version information 
  }
 }
 // Destructor, automatically closes the database , Garbage collection mechanism 
 public function __destruct()
 {
  if(!empty($this->result)){
   $this->free();
  }
  mysql_close($this->conn);
 }//function __destruct();
 /* Get the client real IP address */
 function getip(){
  if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  {
   $ip = getenv("HTTP_CLIENT_IP");
  }
  else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
   $ip = getenv("HTTP_X_FORWARDED_FOR");
  }
  else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  {
   $ip = getenv("REMOTE_ADDR");
  }
  else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
  $ip = $_SERVER['REMOTE_ADDR'];
  }
  else{
   $ip = "unknown";
  }
  return($ip);
 }
}
?>


Related articles: