我有这个使用PHP的mysql查询,第一部分工作正常,但第二部分在网站上有搜索功能根本无法让它正常搜索.这是确切的代码:
<?
$qry = "
SELECT bidprice,timelive,match_title,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE 1 THEN 'WON'
END AS result
FROM game
ORDER BY timelive DESC
";
$searchText = "";
if($_REQUEST['search_text']!=""){
$searchText = $_REQUEST['search_text'];
$qry .=" WHERE game.bidprice LIKE '%$searchText%' OR game.timelive LIKE '%$searchText%'";
}
//for pagination
$starting=0;
$recpage = 10;//number of records per page
$obj = new pagination_class($qry,$starting,$recpage);
$result = $obj->result;
?>
Run Code Online (Sandbox Code Playgroud)
所以考虑搜索我的'游戏'表的上面代码的这部分代码不起作用:
$searchText = "";
if($_REQUEST['search_text']!=""){
$searchText = $_REQUEST['search_text'];
$qry .=" WHERE game.bidprice LIKE '%$searchText%' OR game.timelive LIKE '%$searchText%'";
Run Code Online (Sandbox Code Playgroud)
在我的页面上,当打开此页面并尝试搜索"足球"一词时,我收到此错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'game.bidprice like '%football%' OR game.timelive like '%football%' at line 9
Run Code Online (Sandbox Code Playgroud)
当把一些简单的SELECT语句放在一样的时候,这个搜索功能工作得很好:SELECT*FROM game但是当用完CASE和WHEN语句完成那些更复杂的select查询时我需要它不能运行...
请帮助我正确的搜索功能代码
问题是你不能在ORDER BY之后有一个WHERE子句.您的查询目前看起来像这样:
SELECT bidprice,timelive,match_title,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE 1 THEN 'WON'
END AS finalization
FROM game
ORDER BY timelive DESC WHERE game.bidprice LIKE '%football%' OR game.timelive LIKE '%football%'
^^^^^^^ error
Run Code Online (Sandbox Code Playgroud)
但你需要它看起来像这样:
SELECT
bidprice,
timelive,
match_title,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE 1 THEN 'WON'
END AS finalization
FROM game
WHERE game.bidprice LIKE '%football%' OR game.timelive LIKE '%football%'
ORDER BY timelive DESC
Run Code Online (Sandbox Code Playgroud)
您应该在PHP脚本中添加ORDER BY.尝试这样的事情:
$qry = "
SELECT bidprice,timelive,match_title,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE 1 THEN 'WON'
END AS result
FROM game
";
$searchText = "";
if ($_REQUEST['search_text']!="")
{
$searchText = mysql_real_escape_string($_REQUEST['search_text']);
$qry .= " WHERE game.bidprice LIKE '%$searchText%' " .
" OR game.timelive LIKE '%$searchText%'";
}
$qry .= " ORDER BY timelive DESC";
Run Code Online (Sandbox Code Playgroud)