douji1999 2013-12-14 14:50
浏览 81

使用PHP / MySQL运行SQL查询,然后重新排序结果

I am trying to run a subquery so that I can sort & ouput results according to which tag column a search term appears in. The query runs fine without the subquery but with it I am getting a 'mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given' error. Any help much appreciated.

$sql = "SELECT * FROM (SELECT trackname, tag1, tag2, tag3, tag4, tag5, songtitle, zip_link, album_id, description, album_name, album_desc,  small_logo  FROM albums, tracks WHERE 

        (tag1 = '{$search_tag}' OR
        tag2 = '{$search_tag}' OR
        tag3 = '{$search_tag}' OR
        tag4 = '{$search_tag}' OR
        tag5 = '{$search_tag}')    AND   tracks.album_id = albums.id ) ORDER BY tag1
        ";  

}  else {
    echo '<br/>please enter a search word or phrase<br/><br/>';
    die;
}


        $result = mysqli_query ($mysqli, $sql);
        if($result === false)  { echo 'mysql error'; }
        $count=mysqli_num_rows($result);
        if ($count < 1) {echo '<span class="sorry">sorry - your search returned no results</div><br/>';}    
            else    {   while ($row = mysqli_fetch_array($result)) 
                `       {   
  • 写回答

3条回答 默认 最新

  • dongtangjie0495 2013-12-14 14:58
    关注

    Why are you using a subquery? The following seems to be the query you want:

    SELECT trackname, tag1, tag2, tag3, tag4, tag5, songtitle, zip_link, album_id,
           description, album_name, album_desc,  small_logo
    FROM albums join
         tracks
         on tracks.album_id = albums.id
    WHERE '{$search_tag}' in (tag1, tag2, tag3, tag4, tag5)
    ORDER BY tag1;
    

    Also note the use of proper join syntax and the use of in to simplify the where logic.

    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False