PDA

View Full Version : Putting a calculated variable into a row



Krentenbol
12-04-2011, 09:45 AM
I've been trying around a lot, but I just couldn't figure out how to put the variable into a row. I must forget something...

Code atm:

<?php
include('inc/open.php');
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM status") or die (mysql_error());
while($row = mysql_fetch_assoc($query)){
$uptime_calc = $row['checks'] - $row['down'];
$uptime = round ($uptime_calc/$row['checks']*100,0);
echo $uptime;
echo "<br />";
}
?>
I want $uptime in table status, column uptime.

Old Pedant
12-04-2011, 09:09 PM
So you are saying that the PHP code you show there *DOES* work, but you want move the calculation into the SQL query, instead??

Trivial.


SELECT list, of, other, fields, ROUND(100*(checks-down)/checks) AS uptime
FROM status

Try to avoid using SELECT * when possible. Try to only SELECT the fields you will actually use in your PHP code. Sometimes it won't matter much; other times you can gain as much as 2 or 3 times better performance.

Krentenbol
12-05-2011, 02:37 PM
<?php
include('inc/open.php');
$query = mysql_query("SELECT * FROM status WHERE id = 1") or die (mysql_error());
while($row = mysql_fetch_assoc($query)){
$uptime_calc = $row['checks'] - $row['down'];
mysql_query("SELECT list, of, other, fields, ROUND(100*(checks-down)/checks) AS uptime
FROM status");

}
?>

That's the code now, but my row doesn't update.

Old Pedant
12-05-2011, 08:06 PM
My code was an *EXAMPLE*.

You need to change it to match *YOUR TABLE*.

Surely you don't *REALLY* have fields named "list", "of", "other", and "fields" in your table.

Also, you would REPLACE your query with mine. Not use two queries.