Forum Moderators: coopster

Message Too Old, No Replies

MySql result not showing

         

Go3Team

9:06 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



I'm trying to call up a result in a mysql db. The object of the query, is to call up a maximum result, in this case the maximum speed stored in the db.

The first query gives me a Resource ID #4 result:

$result3 = mysql_query('SELECT * FROM `tracker` ORDER BY `speed` DESC LIMIT 1');

So I added what I found while searching for Resource ID #4:

$result3 = mysql_query('SELECT * FROM `tracker` ORDER BY `speed` DESC LIMIT 1');

$result4 = mysql_fetch_object($result3);

I am not getting the results I need, can anyone help?

I also want to get an average of the data in the speed table, but am stuck on the first problem, can someone help with that?

eelixduppy

9:20 pm on Jan 19, 2007 (gmt 0)



Try something like this:

$result3 = mysql_query('SELECT MAX(speed) as speed FROM `tracker`') or die(mysql_error());
$row = mysql_fetch_array($result3);
echo 'Max: '.$row['speed'];

Go3Team

11:46 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Works great, thank you. Is it possible to get an average from the same table?

eelixduppy

11:53 pm on Jan 19, 2007 (gmt 0)




$result3 = mysql_query('SELECT MAX(speed) as speed,AVG(speed) as avg_speed FROM `tracker`') or die(mysql_error());
$row = mysql_fetch_array($result3);
echo 'Max: '.$row['speed'].'<br/>';
echo 'Avg: '.$row['avg_speed'];

:)

Go3Team

12:30 am on Jan 20, 2007 (gmt 0)

10+ Year Member



You're my new hero. It's for a GPS project, and it sounds like the altitudes and distance travelled part is going to be fun. Thanks again!

[edited by: Go3Team at 12:31 am (utc) on Jan. 20, 2007]

Go3Team

6:49 am on Jan 21, 2007 (gmt 0)

10+ Year Member



Trying to add more info from the search and having some trouble. The same table has a field named which corresponds to the time that the speed was recorded. How do I pull the time at the same time I pull the max and avg speed? Here is what I have so far:

$convert = 1.15077945; //convert knots to mph

$result3 = mysql_query('SELECT MAX(speed) as speed1,AVG(speed) as avg_speed FROM `tracker`') or die(mysql_error());
$row = mysql_fetch_array($result3);
//$time1 = $row['time1'];
$math = $row['speed1'] * $convert; //convert max speed to mph
$math1 = $row['avg_speed'] * $convert; //convert avg speed to mph
echo 'Maximum Speed: '.round($math, 1).'<br/>';
echo 'Average Speed: '.round($math1, 4);
//echo $time1;

I tried modding the query to:

$result3 = mysql_query('SELECT MAX(speed) as speed1,AVG(speed) as avg_speed , time1(time) FROM `tracker`') or die(mysql_error());

I guessing the max speed query will have to be split fromt the avg query, but the syntax isn't working for me.