Monday, November 19, 2012

Keyword Serach in DB Mysql

<?php
function searchAllDB($search){

    global $mysql;

    $out = "";

    $sql = "show tables";
    $rs = mysql_query($sql);
    $num = mysql_num_rows($rs);
    if($num > 0){
   
        while($r = mysql_fetch_array($rs)){
           
           
            $table = $r[0];
            $out .= $table.";";
            $sql_search = "select * from ".$table." where ";
            $sql_search_fields = Array();
            $sql2 = "SHOW COLUMNS FROM ".$table;
            $rs2 = mysql_query($sql2);
             $num2 = mysql_num_rows($rs2);
            if($num2 > 0){
                while($r2 = mysql_fetch_array($rs2)){
               
           
                    $colum = $r2[0];
                    $sql_search_fields[] = $colum." like('%".$search."%')";
                }
                //$rs2->close();
           }
                // echo'<pre>';
            // print_r($sql_search_fields);
            // echo'</pre>';
            $sql_search .= implode(" OR ", $sql_search_fields);
            $sql_search .=';';
            $rs3 = mysql_query($sql_search);
            $res3 = mysql_fetch_assoc($rs3);
           
            echo'<pre>';
            print_r($res3['entity_id']);
            echo'</pre>';
           
        }
      //  $rs->close();
    }

    return $out;
}

searchAllDB('test');



?>