Jump to content

What wrong with this code?


judgenules

Recommended Posts

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/75017-what-wrong-with-this-code/
Share on other sites

<?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'];

}

?>

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>';

?>

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. :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.