MySQL QUERY “LIKE” returns nothing

自作多情 提交于 2019-12-11 18:06:31

问题


I'm using MySQL version 5.0.51a and PHP to access the database, and this query returns nothing when it should return at least 2 rows which match the LIKE condition.

$result = mysql_query("SELECT * FROM user WHERE name LIKE '%".$search."%'OR email LIKE    '%".$search."%' ORDER BY ".$order, $con);

The $search variable is 'Name',

there is no problem with ORDER, $order or $con, i've already tried that, and there are 2 rows where the name is 'Name', but somehow it can't find those rows and it returns nothing.

Does anybody know where the problem is?


回答1:


Try removing double commas

$result = mysql_query("
                    SELECT 
                        * 
                    FROM user 
                    WHERE name LIKE '%{$search}%' OR email LIKE '%{$search}%' 
                    ORDER BY ".$order, $con);



回答2:


It is mainly on the syntax as others have pointed out, here is another way:

$result = mysql_query(" SELECT * FROM user 
WHERE '%{$search}%' IN (name, email )
 ORDER BY ".$order.','. $con)
;



回答3:


Please try this query:

$result = mysql_query("SELECT * FROM user WHERE name LIKE '%".$search."%'
OR email LIKE '%".$search."%' 
ORDER BY ".$order.','.$con);


来源:https://stackoverflow.com/questions/14472673/mysql-query-like-returns-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!