2015-06-27 61 views
0

我有一个工作查询,在我的数据库以及前端成功显示正确的行。目前我的PHP代码:Get_num_rows无法正确显示,如果没有结果

$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';} 

即使有与原始查询发现有两行,只显示如果有什么,但零results..basically它的工作的对面的“没有找到结果”消息我希望如何。

另外$result是我命名查询的变量。 有人可以给我洞察我可能做错了什么吗?

EDIT我使用的查询是:

$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"); 
+1

'的var_dump($行);' –

+2

你能发布您的查询代码? – Saty

+0

我做了编辑与查询 –

回答

0

根据WP文档,get_results方法默认返回对象数组。 (https://codex.wordpress.org/Class_Reference/wpdb

数组没有num_rows属性。只要尝试更改您的代码:

$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

这并不是说你不能使用NUM_ROWS与查询返回一个数组......虽然这工作得很好太