Page 1 of 1

PHP calculations using MySQL results

Posted: Wed Mar 24, 2010 12:08 pm
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

Re: PHP calculations using MySQL results

Posted: Wed Mar 24, 2010 12:47 pm
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.

Re: PHP calculations using MySQL results

Posted: Wed Mar 24, 2010 3:16 pm
by Eran
Or just turn on display_errors