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");
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';}
It's not that you cannot use num_rows with a query returning an array...although this works fine too