问题
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