PHP calculations using MySQL results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
willgooders
Forum Newbie
Posts: 1
Joined: Wed Mar 24, 2010 12:04 pm

PHP calculations using MySQL results

Post by willgooders »

Hi,

I am trying to use information from a database to do some simple calculations. I am struggling with turning the information into variables and then using these variables in some calculations.

Here is the code i have so far. If i delete calc 1, 2 and 3 it will display all the data. Currently it is displaying nothing.

Code: Select all

<?PHP
 
$host="localhost";
$username="root"; 
$password="root"; 
$db_name="research"; 
$tbl_name="users"; 
 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
 
$query  = "SELECT windows, heating FROM users WHERE username ='test'";
$result = mysql_query($query);
 
while($row = mysql_fetch_row($result))
{
    $windows = $row[0];
    $heating = $row[1];
    
    $calc1 = ($heating*0.1);
    $calc2 = ($windows*100);
    $calc3 = (($windows*100)/$heating*0.1));
   
    echo            "<h2>Double Glazing Savings Calculator</h2>" .
            "<p>Number of Windows: $windows</p>" .
            "<p>Yearly Heating Cost: £$heating</p>" .
            "<p>Cost of Replacement: £$calc2</p>" .
            "<p>Annual Saving: £$calc1</p>" .
            "<p>Payback Period: $calc3 years</p>"
}
        
?>
It would be great if i could take the values from my SQL query and turn the individual ones in variables or something that i could then use in some simple php calculations with the results of these being displayed.

Will
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP calculations using MySQL results

Post by AbraCadaver »

You're missing a ; at the end of the echo. You also have unmatched parentheses in your $calc3 expression. Get an editor that does syntax highlighting. It will save you lots of time and grief.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP calculations using MySQL results

Post by Eran »

Or just turn on display_errors
Post Reply