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@company.com'"; while ($row = mysql_fetch_array($result)) { echo $row['MAX(jobID)']; } Thank you Quote 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@company.com'"; $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. Quote 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. Quote 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 Quote 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...? Quote Link to comment https://forums.phpfreaks.com/topic/188933-returning-mysql-max/#findComment-997598 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.