duancuisan2503 2015-06-27 13:38
浏览 57
已采纳

如果没有结果,Get_num_rows无法正确显示

I have a working query that successfully display the correct rows in my db as well as in the front end. Currently my php code is:

$rows = $result->num_rows;
if($rows>=0){
  foreach ($result as $row) {
    echo '<h5 style="background-color:yellow;width:60%"><i>'.$row->company.'</i> can perform your window installation for <i>$'.ROUND($row->newcost,2).'</i><br>';
    echo 'This price includes using <i>'.$row->material.'</i> as your material(s)<br>';
    echo '<hr></h5>';
  }
}else{echo 'No results found';}

Even though there are two rows found with the original query, it displays the 'No results found' message only if there are anything but zero results..basically it's working the opposite of how I want it to.

Also $result is the variable that I named the query. Can someone give me insight on what I could be doing wrong?

EDIT The query I am using is:

$result = $wpdb->get_results( "SELECT `b`.`company` AS `company`,`bp`.`material` AS `material`,
if(((`bp`.`cost` * 1.2) < `ls`.`maximumbid`),(ROUND(`bp`.`cost` * 1.2,2)),ROUND(`bp`.`cost`,2)) AS `newcost` 
from (((`doors_brands_products` `bp` left join `doors_brands` `b` on((`bp`.`brand_id` = `b`.`id`))) 
join `Doors_last_submissions` `ls`) join `doors_materials` `wm`) 
where ((`bp`.`width` = round(`ls`.`width`,0)) 
and (`bp`.`height` = round(`ls`.`height`,0)) 
and (`bp`.`material` = `wm`.`name`) 
and (`bp`.`type` = `ls`.`type`) 
and if((`ls`.`minimumbid` <> '0.00'),(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),(`bp`.`cost` <= `ls`.`maximumbid`)))
ORDER BY b.company ASC");
  • 写回答

2条回答 默认 最新

  • douweihui0178 2015-06-27 14:04
    关注

    According to WP documentation, get_results method returns array of objects by default. (https://codex.wordpress.org/Class_Reference/wpdb)

    Array does not have num_rows property. Simply try to change your code to:

    $rows = $result ? count($result) : 0;
    if($rows>=0){
        foreach ($result as $row) {
            echo '<h5 style="background-color:yellow;width:60%"><i>'.$row->company.'</i> can perform your window installation for <i>$'.ROUND($row->newcost,2).'</i><br>';
            echo 'This price includes using <i>'.$row->material.'</i> as your material(s)<br>';
            echo '<hr></h5>';
        }
    }else{echo 'No results found';}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法