ravens_chance Posted January 18, 2010 Share Posted January 18, 2010 I can get my mySQL query to run fine in phpMyAdmin, but can't get anything when I put it into my PHP code. When I run the query in MySQL in phpMyAdmin I get the result: MAX(jobID) 43 But, when I put this query into my code (with or without a variable) I get nothing. And everything downcode from it doesn't show up. What am I missing? Ideally what I want to work: $result = mysql_query("SELECT MAX(jobID) From job WHERE jobEmail='$jobEmail->email'"); while ($row = mysql_fetch_object($result)) { echo $row->jobID; } mysql_free_result($result); Second attempt at code: $result = mysql_query("SELECT MAX(jobID) FROM job WHERE jobEmail='[email protected]'"; while ($row = mysql_fetch_array($result)) { echo $row['MAX(jobID)']; } Thank you Link to comment https://forums.phpfreaks.com/topic/188933-returning-mysql-max/ Share on other sites More sharing options...
wildteen88 Posted January 18, 2010 Share Posted January 18, 2010 Try using an alias instead $result = mysql_query("SELECT MAX(jobID) as maxJobID FROM job WHERE jobEmail='[email protected]'"; $row = mysql_fetch_array($result); echo $row['maxJobID ']; NOTE: if your query only returns one row/result, then you don't need to use a while loop. Link to comment https://forums.phpfreaks.com/topic/188933-returning-mysql-max/#findComment-997591 Share on other sites More sharing options...
Maq Posted January 18, 2010 Share Posted January 18, 2010 This may be a typo but you're missing the terminating ')' for your mysql_query call. Link to comment https://forums.phpfreaks.com/topic/188933-returning-mysql-max/#findComment-997592 Share on other sites More sharing options...
ravens_chance Posted January 18, 2010 Author Share Posted January 18, 2010 yes that was a typo... Thank you for the Alias recommendation, that fixed it in both scenarios Link to comment https://forums.phpfreaks.com/topic/188933-returning-mysql-max/#findComment-997597 Share on other sites More sharing options...
Maq Posted January 18, 2010 Share Posted January 18, 2010 yes that was a typo... Thank you for the Alias recommendation, that fixed it in both scenarios I always thought you could reference aggregate functions in the result set without an alias...? Link to comment https://forums.phpfreaks.com/topic/188933-returning-mysql-max/#findComment-997598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.