开发者

MySql LIKE returns false if search term is same as entire string in the column, why is that?

So I have following as part of my query

SELECT * FROM $table WHERE columname LIKE '%$searchterm%'

I have tried taking out leading and/or ending wildcards meaning

SELECT * FROM $table WHERE columname LIKE '$searchterm%'

AND

SELECT * FROM $table WHERE columname LIKE '%$searchterm'

AND

SELECT * FROM $table WHERE columname LIKE '%$searchterm%' OR columname LIKE '$searchterm'

and also tried adding following to the query with no luck

OR col开发者_如何学Cumname = '$searchterm'

So when my search term is "myval" and if column has whole string "myval", I would like to have that selected. But ALL of my queries above, return false/return nothing where myval is searchterm and column value as full.

I can not use MATCH because this is not Full-Text index.

EDIT:

PHP Code:

    $sterm = NULL;
            $table = 'mytable';

    if(isset($_GET['s'])) {     $sterm = explode(" ", mysql_real_escape_string($_GET['s']));    }

    if(isset($_POST['s'])) {    $sterm = explode(" ", mysql_real_escape_string($_POST['s']));   }

    if(!empty($sterm)){
        $getdata = "SELECT * FROM $table WHERE termsi != 'Special' "; 

        foreach ($sterm as $value){

            $getdata .= "AND netid_all  LIKE '%$value%' OR  netid_all  = '$value' ";                
        } //End foreach

        $getdata .= "LIMIT 10";

        $result = mysql_query($getdata) or die(mysql_error());
        $row = mysql_fetch_array($result, MYSQL_ASSOC);

        while($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            echo <<<PRINTALL
            {$row[0]}, {$row[1]}, {$row[2]}, {$row[3]}, {$row[4]}, {$row[5]}, {$row[6]}, {$row[7]}, ' <br />'               
PRINTALL;
        } //End While
    } //End If search exists

Okay So As you guys suggested, i tried PHPMyAdmin sql console and it works fine, so it would have to be by PHP!? so here it is.


I'd suggest writing your query building like this:

$fullvalues = array();
$partials = array();
foreach ($sterm as $value){
   $partials[] = "(netid_all LIKE '%" . mysql_real_escape_string($value) . "%')";
   $fullvalues[] = "'" .  mysql_real_escape_string($value) . "'";
}
$partials = implode(' OR ', $partials);
$fullvalues = implode(', ', $fullvalues);

$sql = <<<EOL
SELECT *
FROM $table
WHERE (termsi != 'Special')
   AND (($partials) OR (netid_all IN ($fullvalues));
EOL;

Assuming your search string is a b c, you'd get this query:

SELECT *
FROM yourtable
WHERE (termsi != 'Special')
   AND (((netid_all LIKE '%a%') OR (netid_all LIKE '%b%') OR (netid_all LIKE '%C%')) OR (netid_all IN ('a', 'b', 'c')))

If your search requires that all terms be present, then change the 'OR' to 'AND' in the implode.


Well found it,

$row = mysql_fetch_array($result, MYSQL_ASSOC);

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))

Was the problem, earlier when I was testing things, anyhow, it should have been the following

$row = mysql_fetch_array($result, MYSQL_ASSOC);

    while($row)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜