dongruyan4948 2011-11-01 17:39
浏览 49
已采纳

搜索功能在我的PHP MySQL查询中不起作用

I have this mysql query using PHP and first part is working fine, but second part where have search function on site simply cannot get it workable to search properly. Here is exact code:

<?
$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;

?>

So this part of code from above code which is considering searching my 'game' table is not working:

$searchText = "";
if($_REQUEST['search_text']!=""){
    $searchText = $_REQUEST['search_text'];
$qry .=" WHERE game.bidprice LIKE '%$searchText%' OR game.timelive LIKE '%$searchText%'";

On my page I receive this error when open this page and try to search for word 'football':

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

This search function is working fine when put some simple SELECT statement something like: SELECT * FROM game but when done those more complex select query with CASE and WHEN statements which i need its not functioning...

Please help me with proper code for proper search function

  • 写回答

2条回答 默认 最新

  • dtxa49711 2011-11-01 17:41
    关注

    The problem is that you can't have a WHERE clause after the ORDER BY. Your query currently looks like this:

    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
    

    But you need it to look like this:

    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
    

    You should add the ORDER BY afterwards in your PHP script. Try something like this:

    $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";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献