dongshen2903 2012-09-12 04:43
浏览 20
已采纳

PHP搜索生成错误

I have a search field feature on my website, with below PHP code:

if(isset($_GET['search_ti']) && $_GET['search_ti'] != "")
{
    $search_ti = preg_replace('#[^a-z 0-9?!-]#i', '', $_GET['search_ti']);

    $sqlCommand = "
    SELECT * FROM page WHERE title LIKE '%$search_ti%' OR body LIKE '%$search_ti%'
    ";

    $result = $mysqli->query($sqlCommand) or die(mysql_error());
    ...

The URL variable search_ti is the keyword for the search, which I got from the URL. For example:

searchResult.php?search_ti=home

If the keyword is found in my MySQL table, the result shows and no errors.

But if the keyword is not found, the web browser generates below error:

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 '-4,4' at line 1

What is wrong with my code?

ADDITIONAL INFO:

The complete PHP code: http://jsfiddle.net/eQSZc/

The keyword: home. The error only shows if the keyword is not found in my table.

  • 写回答

2条回答 默认 最新

  • douzhaobo6488 2012-09-12 05:49
    关注

    Change your pagination_mechanism_and_buttons_variables_for_Search.php to the one in http://jsfiddle.net/GsNmw/1/

    I have added the following block just before you construct the $limit and change the $limit as well.

    $start = ($pn - 1) * $itemsPerPage;
    
    if($start<0){
        $start = 0;
    }
    
    $limit = 'LIMIT ' .$start .',' .$itemsPerPage;
    

    This avoids negative limit and hence avoids your error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致