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

}

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.