[英]MySQL query returning rows from every user
I have a simple search script to search a database which should return all the rows of data that match the search query that belong to that user, however my query (below) is returning rows from every user. 我有一个简单的搜索脚本来搜索数据库,该数据库应该返回与属于该用户的搜索查询匹配的所有数据行,但是我的查询(下面)是从每个用户返回的行。 Any idea how to fix this?
知道如何解决这个问题吗?
SELECT * FROM tablename
WHERE title LIKE '%" . $q . "%'
OR text LIKE '%" . $q . "%'
AND user='$user'
Side Note : This is executed in PHP so the $q
is the variable that holds the query. 附注 :这是在PHP中执行的,因此
$q
是保存查询的变量。
你试过这个吗?
"SELECT * FROM tablename WHERE (title LIKE '%".$q."%' OR text LIKE '%".$q."%') AND user='$user'"
在尝试SQL查询之前,请清除任何空格的$ user变量
Try this: 尝试这个:
<?php
...
echo "SELECT * FROM tablename
WHERE title LIKE '%" . $q . "%'
OR text LIKE '%" . $q . "%'
AND user='$user'";
And see what the output looks like. 看看输出是什么样的。
Just as an aside, you can probably get away with 除此之外,你可以侥幸逃脱
WHERE title LIKE '%$q%'
(ie, not having to concatenate the query string). (即,不必连接查询字符串)。 This will keep your code a bit cleaner and reduce errors (from missing a period/quote).
这将使您的代码更清洁并减少错误(从错过句点/引用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.