Search Using LIKE and OR
I have this 开发者_StackOverflow社区code:
if(!isset($_GET['query'])) {
echo "<h1>No search query specified.</h1><a href='index.php'>Return</a>";
} else {
$query = trim($_GET['query']);
$query = mysql_real_escape_string($query);
$query = mysql_query("SELECT * FROM warps
WHERE name LIKE '%$query%'
OR desc LIKE '%$query%' OR keywords LIKE '%$query'");
if($query) {
while($row = mysql_fetch_assoc($query)) {
echo $row['name'];
}
} else {
echo "<h1>No results found</h1>";
}
}
Why is it not returning any results when I enter a search query which corresponds to information in my database?
You must not call a field desc. Or if you call it like that use it like this `desc`. DESC is a command so in your query MySQL thinks you want something to be decreasing.
Try to "var_dump" your query and the result, you get back. Make sure you have error reporting enabled:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Your query as already mentioned, contains the MySQL-Keyword DESC as column name. If there is no way to rename that column, you may enclose it in backticks like this:
`desc`
For whatever reason, you have opted not to delimit your field names with backticks.
You can often get away with this, but in your case you cannot because DESC
is a keyword.
Write:
SELECT * FROM `warps`
WHERE `name` LIKE '%$query%'
OR `desc` LIKE '%$query%'
OR `keywords` LIKE '%$query'
精彩评论