PHP SEARCH CLASS LOCAL DATABASE SQL

    Rating: ★★★★☆
    View: 254
    Download: 174
    Từ khóa:

    The class returns an array with the values of primary keys of the table records returned by the queries. Duplicate record entries are eliminated.

    HTML Markup in search file:

    
    

    PHP Markup in search file:

    if(isset($_POST['submit']))
    {
    include("mysql_search.class.php");
    $mysqlSearchAndReplace = new mysqlSearchAndReplace("demo", "localhost", "root", "xxx");
    $_search=$mysqlSearchAndReplace->clean_data($_POST['search']);
    echo'
    
    ';
    }

    Attention: If we want to search via many tables that we can call search function like this:

    $mysqlSearchAndReplace->search($_search, array('table one','table two','...'))

    SQL need to create:

    CREATE TABLE IF NOT EXISTS `mysearch` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(60) NOT NULL,
      `message` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

    And the last search.class.php with search function like this:

     function search($search, $tables = array())
    	{
    
            if(count($tables) == 0){
                $tables = $this->getTables();
            }
    		$stt=0;
    		$output='';
    		$all_total=0;
    		$result='';
    
            foreach ($tables as $table)
            {
                
                $fields = $this->columns($table);
                $fields_names = $this->columns($table, true);
    
                $where = implode('` LIKE "%'.$search.'%" OR `', $fields_names);
                $sql = "SELECT * FROM ".$table.' WHERE `'.$where.'` LIKE "%'.$search.'%"';
    
                $query = @mysql_query($sql);
                $total = mysql_num_rows($query);
    			$all_total=$all_total+$total;
                if ($all_total > 0)
                {
                    while ($row = mysql_fetch_array($query, MYSQL_ASSOC))
    				{
                        $result.= "".$stt++.". ".$row["name"]."
    ";//edit code here } } } if($result!='') { $output="

    Found ".$all_total." result:


    ".$result; } else { $output="

    Found 0 result

    "; } return $output; }

     

    loading Đang tải...

    template được ưa chuộng