#native_company# #native_desc#
#native_cta#

MySQL Searching Class

By Sam McKone
on November 26, 2001

Version: v.1.3

Type: Full Script

Category: Databases

License: GNU General Public License

Description: Search a mysql database without writing a query.

//============================================ 
// NEWS 
               **future updates** 
 1) porting to other databases like postgreSQL and MSSQL 
 2) function to tie searches together (may be a diffrent script) 
  
//============================================ 
// Function List 
void = load_properties((string user_name,string password,string host_add,string database_ name); 

void = connect(void); 

// note the column can be an array when the table is a string 
// this allows for searchs of more than one column in a table 
// seting the column name to ALL_COLUMNS will make the script 
// search all the columns of that table (if sensitive information  
// is in that table do not do this). 
void = load_tables(string table_name,string column_name||array); 
void = load_tables(array table_names,array column_names||array); 

void = add_table(string table_name,string column_name||array); 
void = add_table(array table_names,array column_names||array); 

void = all_columns(int table_num); 
void = all_columns(string table_name); 

void = exec(string search_text); 

void = free_results(void); 
void = close(void); 

array = get_row(void); 

void = set_row_pos(int);

array = get_exact_row(int row_number); 

int = num_rows(void); 

void = delete_row(int row_number); 

//============================================ 
// sample of use 

$search = new db_search("user name","password","sql.php.net","this site"); 

$tables = array("name","forum","text"); 
$columns = array("column","column","body"); 

$search->load_tables($tables,$columns); 
$search->add_table("another","column"); 

$search->exec("search for"); 
$i = 0; 
while($i < $search->num_rows()){ 
   $row = $search->get_row(); 
   for($n = 0;$n < count($row);$n++){ 
      echo "$row[$n] "; 
   } 
   $i++; 
   echo "<br>"; 
} 

// complex use of db_search 
$search = new db_search("dev","not gonna tell you","","devsite"); 

$tables = array("users","articles"); 
$s = array("body","subject"); 
$columns = array("body","body",); 

$search->load_tables($tables,$columns); 
$search->add_table("forum_topic",$s); 

$search->exec("php"); 

echo $search->num_rows(); 

<?php 
//======================================================= 
// db_search class can be scaled for any database system 
// you might have. 
// (Please send any updates to [email protected]) 
// 
// Sam McKone 
// Oct. 2001 
//======================================================== 

class db_search{ 
  //======================================================= 
  // data members 
  //======================================================= 
  var $tables = ""; 
  var $field = ""; 
  var $results = ""; 
  var $result_loc = 0; 
   
  var $host = ""; 
  var $user_name = ""; 
  var $pass_word = ""; 
  var $data_base = ""; 
   
  var $connection = ""; 
  //======================================================= 
  // constructors 
  //======================================================= 
  function db_search($set_user,$set_pass,$set_host,$set_db_name){ 
    $this->load_properties($set_user,$set_pass,$set_host,$set_db_name); 
    $this->connect(); // start the mySQL connection 
  } 
  //=======================================================
  // Connection and initialization functions 
  //======================================================= 
  function load_properties($set_user,$set_pass,$set_host,$set_db_name){ 
    $this->user_name = $set_user; 
    $this->pass_word = $set_pass; 
    $this->data_base = $set_db_name; 
    $this->host = $set_host; 
  } 
  function connect(){ 
     if(!$this->host){ 
       $this->error("Host was not set"); 
     }elseif(!$this->user_name){ 
       $this->error("User was not set"); 
     }elseif(!$this->pass_word){ 
       $this->error("Password was not set"); 
     } 
      
     $this->connection = mysql_connect($this->host, 
                         $this->user_name,$this->pass_word); 
     if(!$this->connection){ 
        $this->error("Connection could not be established"); 
     } 
  } 
  //========================================================= 
  // Table functions 
  //========================================================= 
  // warning running load_tables function more than once 
  // will overwrite prevously entered tables, to add another 
  // table use the add_table function 

  function load_tables($set_tables,$set_field){ 
    $this->tables = $set_tables; 
    $this->field = $set_field; 
    $this->scan_tables(); 
  } 
  function add_table($new_table,$set_field){ 
    if(is_array($new_table) AND is_array($set_field)){ 
      $i = count($this->tables); 
      for($j = 0;$j < count($new_table);$j++,$i++){ 
         $this->tables[$i] = $new_table[$j]; 
         $this->field[$i] = $set_field[$j]; 
      } 
    }elseif(is_string($new_table)){ 
      $i = count($this->tables); 
      $this->tables[$i] = $new_table; 
      $this->field[$i] = $set_field; 
    }else{ 
       $this->error("Pass a string or an array of strings"); 
    } 
    $this->scan_tables(); 
  } 
  function all_columns($table){ 
     if(is_string($table)){ 
        $temp = $table; 
        $table = 0; 
        while($this->tables[$table] != $temp && $table < count($this->tables)){ 
            $table++; 
        } 
     } 
     if($table >= count($this->tables)){ 
         // if table was not yet entered 
         $this->error("table <b>".$temp."</b> is not in the records."); 
         return false; 
     } 
     if($this->connection){ 
         $i = 0; 
         $query = "SHOW FIELDS FROM ".$this->tables[$table].";"; 
         $tempory = mysql_db_query($this->data_base,$query,$this->connection); 
         $this->field[$table] = array(); 
         while($row = mysql_fetch_assoc($tempory)){ 
            $this->field[$table][$i] = $row[Field]; 
            $i++; 
         } 
     }else{ 
        $this->error("function <b>all_columns()</b> can only be run after " 
                     ."a connection is made."); 
     } 
  } 
  function scan_tables(){ 
     for($i = 0;$i < count($this->tables);$i++){ 
        if($this->field[$i] == "ALL_COLUMNS"){ 
            $this->all_columns($i); 
        } 


     } 
  } 
  //========================================================= 
  // exacution functions 
  //========================================================= 
  function exec($search_string){ 
    if($this->connection){ 
      if($this->results){ 
         $loc = count($this->results); 
      }else{ 
         $loc = 0; 
      } 
      for($i = 0; $i < count($this->tables);$i++){ 
          if(is_array($this->field[$i])){ 
            $query = "SELECT * FROM ".$this->tables[$i]." WHERE "; 
            $query .= $this->field[$i][0]; 
            $query .= " LIKE '%$search_string%'"; 
            for($n = 1;$n < count($this->field[$i]);$n++){ 
              $query .= " OR ".$this->field[$i][$n]; 
              $query .= " LIKE '%$search_string%'"; 
            } 
          }else{ 
            $query = "SELECT * FROM ".$this->tables[$i]." WHERE "; 
            $query .= $this->field[$i]." "; 
            $query .= "LIKE '%$search_string%';"; 
          } 
          $tempory = mysql_db_query($this->data_base,$query,$this->connection); 

          while($row = mysql_fetch_array($tempory)){ 
            $this->results[$loc] = $row; 
            $loc++; 
          } 
       } 
    }else{ 
       $this->error("function <b>exec()</b> can only be run after " 
                     ."a connection is made."); 
    } 
  } 
  //========================================================= 
  // memory release functions 
  //========================================================= 
  function close(){ 
    $this->tables = ""; 
    $this->field = ""; 
    $this->host =""; 
    $this->user_name = ""; 
    $this->pass_word = ""; 
    $this->free_results(); 
    $this->data_base = ""; 
    $this->connection = ""; 
  } 
  function free_results(){ 
    $this->results = ""; 
    $this->result_loc = 0; 
  } 
  //======================================================= 
  // Result functions 
  //======================================================= 
  function set_row_pos($pos){ 
     if($pos <= count($this->results)){ 
        $this->result_loc = $pos; 
     } 
  } 
  function get_row(){ 
    return $this->results[$this->result_loc++]; 
  } 
  function get_exact_row($row_num){ 
    return $this->results[$row_num]; 
  } 
  function num_rows(){ 
    $count = 0; 
    while($this->results[$count])$count++; 
    return $count; 
  } 
   // Warning deleting rows will change the row order. 
  function delete_row($row_num){ 
    for(;$row_num < count($this->results);$row_num++){ 
       $this->results[$row_num] = $this->results[$row_num + 1]; 
    } 
    $this->results[$row_num] = 0; 
  } 
  //======================================================= 
  // Internal functions 
  //======================================================= 
  function error($error_text){ 
     echo $error_text . "<br>"; 
  } 
} 

?>