Hot Whiskey Hot Whiskey - 4 months ago 7
PHP Question

mySQL min() and max() query array echoing zero

I have a table set up with 5 columns:


user_id, 5k, 10k, halfmarathon, and marathon.


I want to display the user's best times on a user page for each distance of run. The user will be able to update any of the times which creates a new row and the rest of the columns are set to null. So for example,

Row 1 is:


user_id: 5, 5k: null, 10k: 45:00, half: null, marathon: null.


Then the user runs another 10k and gets a better time, plus wants to update their 5k time :

Row 2 is then:


user_id: 5, 5k: 15:53, 10k: 40:40, half: null, marathon: null.


When I run the following SQL query

$query = "SELECT MIN(5k), MIN(10k), MIN(halfmartahon), MIN(marathon)
FROM Times
WHERE user_id = ".$userID."
GROUP BY user_id";
$db->query($query);
//Assign Result Set
$user_benchmarks = $db->single();`


I get an array that is correct when I
vardump()
(I am storing the times in seconds) :


object(stdClass)#18 (4) { ["MIN(5k)"]=> string(3) "953" ["MIN(10k)"]=> string(4) "2440" ["MIN(halfmarathon)"]=> string(1) "0" ["MIN(marathon)"]=> string(1) "0" }


However, when I try to echo this, so
$user_benchmarks->5k
it doesn't show anything and when I run
print_r($user_benchmarks->5k)
it comes back as
NULL
.

Has anyone encountered this / know what's happening? I've also tried turning the string to an integer before printing it to no avail - still get
NULL
.

Answer

Use

SELECT MIN(5k) as 5k, 
       MIN(10k) as 10k, 
       MIN(halfmartahon) as halfmartahon, 
       MIN(marathon) as marathon