Forum Moderators: coopster

Message Too Old, No Replies

What's wrong with this code?

Trying to call a function.

         

judgenules

4:21 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



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.

cameraman

8:08 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your problem's here:
$alltotal = $row['total'];

$row['total'] is local to the function - use $result that you got back from the function call.