woodsonoversoul Posted August 2, 2007 Share Posted August 2, 2007 Can anyone please explain why this doesn't return the sum of the values stored in "price"... // Compute total spent $sql = "SELECT SUM(price) AS total FROM purchase"; if ($result = $mysqli->query($sql)) { if ($mysqli->num_rows()) { $row = $result->fetch_assoc(); echo $row['total']; } } else { echo $mysqli->error; Quote Link to comment https://forums.phpfreaks.com/topic/63112-solved-problem-using-sum/ Share on other sites More sharing options...
woodsonoversoul Posted August 2, 2007 Author Share Posted August 2, 2007 When I use that code I get: Fatal error: Call to undefined method mysqli::num_rows() All help is greatly appreciated. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/63112-solved-problem-using-sum/#findComment-314442 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 try // Compute total spent $sql = "SELECT SUM(price) AS total FROM purchase"; if ($result = $mysqli->query($sql)) { if ($result->num_rows) { $row = $result->fetch_assoc(); echo $row['total']; } } else { echo $mysqli->error; Quote Link to comment https://forums.phpfreaks.com/topic/63112-solved-problem-using-sum/#findComment-314460 Share on other sites More sharing options...
woodsonoversoul Posted August 2, 2007 Author Share Posted August 2, 2007 Thanks so much! That totally fixed it. Could you you go one step further and explain why that code works and mine didn't? I mean I see the parenthesis were removed but could you go into a little more detail? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/63112-solved-problem-using-sum/#findComment-314497 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 u use $mysqli but i use $result if ($result->num_rows) because its already passed by that and remove () ok hope that helps mark this as solved Quote Link to comment https://forums.phpfreaks.com/topic/63112-solved-problem-using-sum/#findComment-314502 Share on other sites More sharing options...
woodsonoversoul Posted August 3, 2007 Author Share Posted August 3, 2007 It does. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/63112-solved-problem-using-sum/#findComment-314527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.