MySQL Forums
Forum List  »  PHP

num_rows not working when used SUM()
Posted by: Onkar Shinde
Date: February 28, 2015 12:32AM

Hi,

I want to output a simple result. I join 2 tables and SUM one column in one, i get the SUM results correct but cannot get the num_rows proper.

My Code:


$clientid = 24;

$query = "SELECT items.itemid, SUM(prices.priceid) as PR FROM items 
JOIN prices ON items.itemid = prices.itemid 
WHERE items.clientid = '$clientid'";


$result =  $db_connect -> query($query);
$row_cnt = $result -> num_rows; 

echo $joinrow_cnt."<br/><br/><br/>";

while($assocresult = mysqli_fetch_assoc($result)){
    
    $price = $assocresult['price'];
    $sumprice = $assocresult['PR'];
    $item = $assocresult['itemid'];
    $realprice = ($price/100)*80;        //after 80% discount
    
    echo "ItemId: ".$item." Price : " .$price." real price: " .$realprice."<br/>";
    echo "Sum of all items: " .$sumprice;
  
}


I only get one line of result and the $row_cnt value is only 1.

I want to be able to display the number of rows where items.clientid = '$clientid' and want to SUM(prices.priceid).

P.S. The column 'priceid' contains the actual price.

Thanks in advance !!

Options: ReplyQuote


Subject
Written By
Posted
num_rows not working when used SUM()
February 28, 2015 12:32AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.