Search code examples
phpmysqlsqlmysqlisql-like

php MySql query `LIKE '$word'` returns NULL


In the code below the

var_dump($row);

shows NULL

$query = "SELECT * FROM maintable WHERE Category LIKE '$word'
                                                        OR Title LIKE '$word'
                                                        OR Title2 LIKE '$word'
                                                        OR Description LIKE '$word'
                                                        OR Description2 LIKE '$word'
                                                        OR Preimushestva LIKE '$word'
                                                        OR Preimspisok LIKE '$word';";

            $result = mysqli_query($link, $query)
            or die("Error: ".mysqli_error($link));

            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

            var_dump($row);

and $word = 'dolor' and I have word dolor in my table, in Description tab help me find mistake please


Solution

  • You're missing your pattern matching characters. Without them LIKE is essentially the same as =.

    Here's an example:

    $query = "SELECT * FROM maintable WHERE Category LIKE '%$word%'
                                                        OR Title LIKE '%$word%'
                                                        OR Title2 LIKE '%$word%'
                                                        OR Description LIKE '%$word%'
                                                        OR Description2 LIKE '%$word%'
                                                        OR Preimushestva LIKE '%$word%'
                                                        OR Preimspisok LIKE '%$word%';";