public function select()


หน้าแรก PHP MySQL เกร็ดความรู้ public function select()

public function select()

This is the first function where things begin to get a little complicated. Now we will be dealing with user arguments and returning the results properly. Since we don’t necessarily want to be able to use the results right away we’re also going to introduce a new variable called result, which will store the results properly. Apart from that we’re also going to create a new function that checks to see if a particular table exists in the database. Since all of our CRUD operations will require this, it makes more sense to create it separately rather than integrating it into the function. In this way, we’ll save space in our code and as such, we’ll be able to better optimize things later on. Before we go into the actual select statement, here is the tableExists function and the private results variable.

  1. private $result = array();   
  2.   
  3. private function tableExists($table)  
  4.     {  
  5.         $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');  
  6.         if($tablesInDb)  
  7.         {  
  8.             if(mysql_num_rows($tablesInDb)==1)  
  9.             {  
  10.                 return true;  
  11.             }  
  12.             else  
  13.             {  
  14.                 return false;  
  15.             }  
  16.         }  
  17.     }  

This function simply checks the database to see if the required table already exists. If it does it will return true and if not, it will return false.

  1. public function select($table$rows = '*'$where = null, $order = null)  
  2.     {  
  3.  

    ขึ้นไปด้านบน