dongmao3131 2013-12-01 18:32
浏览 38
已采纳

mysqli查询语句不起作用

I have written a mysqli statement for advanced search of my website. the generated query does not work. here is the generated query:

 SELECT title, stars, pic_main, id, province FROM ads WHERE 'province' = 8 AND( title LIKE %شریف% OR content LIKE %شریف% OR keywords LIKE %شریف% OR title LIKE %شریف%)

And here is the code that I use to execute the query:

if($transorder = $site_db->query($statement))
{
    echo "True";
    $i=0;
    while($row_obj = $transorder->fetch_object())
    {
        $item[$i]['id'] = $row_obj->id;
        $item[$i]['pic_main'] = $row_obj->pic_main;
        $item[$i]['title'] = $row_obj->title;
        $item[$i]['province'] = $row_obj->province;
        $item[$i]['stars'] = $row_obj->stars;       
        $i++;
    }
}
else
{
    echo "False";
}
  • 写回答

1条回答 默认 最新

  • dsvs50005 2013-12-01 18:33
    关注

    You need quotes around text in your LIKE statements, also I notice you are using title 2 times. If you use single quote (province), MySQL might expect you are comparing the String 'province' with the value 8 you should use ` around the 'province':

     SELECT title, stars, pic_main, id, province 
     FROM ads 
     WHERE `province` = 8 
           AND ( title LIKE '%شریف%' 
                 OR content LIKE '%شریف%' 
                 OR keywords LIKE '%شریف%' 
                 OR title LIKE '%شریف%')
    

    To find out what error MySQL is returning you can use echo $site_db->error;.

    The Solution was

    $site_con->set_charset("utf8");
    

    Because of the use of the use of none default characters of the MySQL connection. So the connection should be set to utf8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号