Non so come far cercare un intera stringa in una query specificata!

  • Creatore Discussione Creatore Discussione ditex
  • Data di inizio Data di inizio

ditex

Nuovo Utente
9 Apr 2012
11
0
0
Salve a tutti,
questo script che posterò è un motore di ricerca su query di un database, io lo editato al fine di usarlo come tracking di ordini commerciali per un mio sito web.
Il problema è che quando nel form metto ad. esempio il numero ordine "1" mi trova l'ordine es. "4837190", mentre io vorrei che se digita il numero 1 deve trovare esclusivamente l'ordine numero 1....

CODICE SORGENTE:

file class.search.php:

Codice:
<?php

class search_engine
{
    function search_engine($mysql)
    {
        # set database connection
        $this->host = $mysql[0];
        $this->username = $mysql[1];
        $this->password = $mysql[2];
        $this->database = $mysql[3];
        $this->link = mysql_connect($this->host,$this->username,$this->password) or die(mysql_error());
        $this->db_selected = mysql_select_db($this->database,$this->link) or die(mysql_error());
        $this->found = array();
    }
    function set_table($table)
    {
        # set table
        $this->table = $table;
    }
    function set_keyword($keyword)
    {
        # set keywords
        $this->keyword = explode(" ", $keyword);
    }
    function set_primarykey($key)
    {
        # set primary key
        $this->key = $key;
    }
    function set_fields($field)
    {
        # set fieldnames to search
        $this->field =$field;
    }
    function set_dump()
    {
        # var dump objects
        echo '<pre>';
        var_dump($this->found);
        echo '</pre>';
    }
    function set_total()
    {
        # total results found
        return sizeof($this->found);
    }
    function set_result()
    {
        # find occurence of inputted keywords
        $key =  $this->key;
        for ($n=0; $n<sizeof($this->field); $n++)
        {
            for($i =0; $i<sizeof($this->keyword); $i++)
            {
                $pattern = trim($this->keyword[$i]);
                $sql = "SELECT * FROM ".$this->table." WHERE `".$this->field[$n]."` LIKE '%".$pattern."%'";
                $result = mysql_query($sql);
                while ($row = mysql_fetch_object($result) AND !empty($pattern))
                {
                    $this->found[] = $row->$key;
                }
            }
        }
        $this->found = array_unique($this->found);
        return $this->found;
    }
}

?>

Grazie mille!
 
Ciao,

prova cmabiando
PHP:
LIKE '%".$pattern."%'
con
PHP:
= '".$pattern."'
 
LIKE significa " che contiene " poi si affida alle % per sapere come cercare la stringa

ciao
 

Discussioni simili