简体   繁体   中英

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.

你试过这个吗?

"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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM