Divisions in consultation to table

0

I'm starting with MySQL and Highcharts , to show graphs of some sensors.

The first thing I do is a query to save the data in a array . I do it like this:

<?php 
$con = mysql_connect("localhost", "", ""); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
}

mysql_select_db("database", $con); 
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result)) 
{ 
    echo $row['Time'] . "\t" . $row['Probe1']. "\n"; 
} 

mysql_close($con); 
?>

The fact is that I need the data in the Probe1 column to be divided by 10 (because the program sends them to me without decimals). I have tried several ways, but I do not get it (like doing a query of your own within $row ). I think I've complicated my life and I'm sure it's something much simpler. Can you lend me a hand?

    
asked by Alex 03.03.2017 в 22:20
source

1 answer

0

If all you are looking for is dividing the field, just add the division at the time of use

    <?php 
$con = mysql_connect("localhost", "", ""); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
}

mysql_select_db("database", $con); 
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result)) 
{ 
    echo $row['Time'] . "\t" . ($row['Probe1']/10). "\n"; 
} 

mysql_close($con); 
?>

Although the correct way would be to not make a select * to get two fields and do it better in the following way

 <?php 
$con = mysql_connect("localhost", "", ""); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
}

mysql_select_db("database", $con); 
$result = mysql_query("SELECT Time,(Probe1/10) as Probe1 FROM table");
while($row = mysql_fetch_array($result)) 
{ 
    echo $row['Time'] . "\t" . $row['Probe1']. "\n"; 
} 

mysql_close($con); 
?>
    
answered by 10.04.2017 / 11:52
source