Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Putting a calculated variable into a row

    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:
    Code:
    <?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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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.
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    <?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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •