douji1058 2012-04-22 00:25
浏览 11
已采纳

查询结果不会被回应

I have a function. in that function in have a select query. another query should return the number of results that will be in the first query. I have this code:

    function name ($f){  
     global $db;
     ...
     $results = "SELECT `a` FROM `b` WHERE $where"; 
     $results_num = ($query = mysqli_query($db, $results)) ? mysqli_num_rows($results) : 0; 

     echo $results;
     echo $results_num;
    }

will echo:

SELECT `a` FROM `b` WHERE `keywords` LIKE '%abc%'0 

what is just $results but not $results_num? I do not understand why echo $results_num wont be shown and why there is a 0 at the end of $results so if there is someone who could give me advise to solve this I really would appreciate. thanks a lot.

  • 写回答

3条回答 默认 最新

  • duanmeng3476 2012-04-22 00:37
    关注

    Firstly, $results_num is zero and is being output (that's why there's a 0 at the end of your output). That's because this code is wrong:

    $results_num = ($query = mysqli_query($db, $results)) ? mysqli_num_rows($results) : 0; 
    

    Breaking it out a little:

    $query = mysqli_query($db, $results);
    $results_num = $query ? mysqli_num_rows($results) : 0;
    

    And the second line should actually be:

    $results_num = $query ? mysqli_num_rows($query) : 0;
    

    You need to pass the query handle to mysqli_num_rows(), not the SQL.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀