judgenules Posted October 27, 2007 Share Posted October 27, 2007 Hi there, I'm trying to get a function called, where it adds up all the total costs from the table. Below is the code: <?php function get_total_price() { //sum total price for all items in inventory $query = "select SUM(inventory.price*inventory.qty) AS subtotal, SUM(subtotal) AS total FROM inventory"; $result = mysql_query($query); $row = mysql_fetch_row($result); return $row['total']; } ?> Here is the code to call the function: <? require('calculation.php'); $result = get_total_price(); ?> <? $alltotal = $row['total']; echo '<div id ="title">Total Earned for the day: $'.$alltotal.'</div>'; ?> When i execute the script, it gives me an error where it says Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\website1\calculation.php on line 12. I deleted 'SUM(subtotal) AS total', and it didnt give me any errors but it only shows a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/ Share on other sites More sharing options...
MadTechie Posted October 27, 2007 Share Posted October 27, 2007 <?php function get_total_price() { //sum total price for all items in inventory $query = "select SUM(inventory.price*inventory.qty) AS subtotal, SUM(subtotal) AS total FROM inventory"; $result = mysql_query($query) or die(mysql__error()); //Add error handling $row = mysql_fetch_row($result); return $row['total']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379309 Share on other sites More sharing options...
judgenules Posted October 27, 2007 Author Share Posted October 27, 2007 Hi, thanks for your respond. I added the error handling code but it still wont give me any data. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379322 Share on other sites More sharing options...
MadTechie Posted October 27, 2007 Share Posted October 27, 2007 what error did you get ? if any ? Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379323 Share on other sites More sharing options...
MadTechie Posted October 27, 2007 Share Posted October 27, 2007 also <? require('calculation.php'); $result = get_total_price(); ?> <? $alltotal = $row['total']; echo '<div id ="title">Total Earned for the day: $'.$alltotal.'</div>'; ?> should be <? require('calculation.php'); $alltotal = get_total_price(); echo '<div id ="title">Total Earned for the day: $'.$alltotal.'</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379326 Share on other sites More sharing options...
Cagecrawler Posted October 27, 2007 Share Posted October 27, 2007 In the error-handling code: mysql__error() should be: mysql_error() Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379328 Share on other sites More sharing options...
judgenules Posted October 27, 2007 Author Share Posted October 27, 2007 It says Unknown column 'subtotal' in 'field list'. And i deleted 'SUM(subtotal) AS total' again but it still shows blank. I also tried editing to the one you told me to, but it still shows nothing. Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379329 Share on other sites More sharing options...
peranha Posted October 27, 2007 Share Posted October 27, 2007 Go to the Apache error log, and is there any errors in there? Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379474 Share on other sites More sharing options...
judgenules Posted October 28, 2007 Author Share Posted October 28, 2007 No, there's no errors in the log. Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379634 Share on other sites More sharing options...
trq Posted October 28, 2007 Share Posted October 28, 2007 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379639 Share on other sites More sharing options...
judgenules Posted October 28, 2007 Author Share Posted October 28, 2007 Here's the current code: <?php function get_total_price() { //sum total price for all items in inventory $query = "select SUM(inventory.qty*inventory.price) AS 'total' FROM inventory"; $result = mysql_query($query) or die(mysql_error()); //error handling $row = mysql_fetch_row($result); return $row['total']; } ?> I've made some changes to the codes to call the function. Here it is: <? require('calculation.php'); $alltotal = get_total_price(); echo '<div id ="title">Total Earned for the day: $'.$alltotal.'</div>'; ?> Thanks for your respond. Quote Link to comment https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/#findComment-379646 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.