Search code examples
mysqlsearchsql-like

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 columname = '$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.


Solution

  • 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)