Speedysnail6 Posted September 8, 2013 Share Posted September 8, 2013 Hello, This is my PHP/MySQL code. $result = mysqli_query($con,"SELECT MAX(ID) FROM lessons"); while($row = mysqli_fetch_array($result)) { echo $row['ID']; } And yet, nothing is entered on the page. What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted September 8, 2013 Solution Share Posted September 8, 2013 You need a column alias for the MAX expression. Try SELECT MAX(ID) as ID .... Quote Link to comment Share on other sites More sharing options...
Speedysnail6 Posted September 8, 2013 Author Share Posted September 8, 2013 Thank you! That worked! I appreciate it Quote Link to comment Share on other sites More sharing options...
vinny42 Posted September 8, 2013 Share Posted September 8, 2013 FYI: you don't absolutely need the alias, it just makes life easier. The data you select becomes available as the expression that you selected, so echo $row['MAX(ID)']; would also work, but it's not pretty, it promotes bugs, it's bad. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 8, 2013 Share Posted September 8, 2013 or use echo $row[0]; since fetch_array() is being used. Quote Link to comment Share on other sites More sharing options...
vinny42 Posted September 8, 2013 Share Posted September 8, 2013 or use echo $row[0]; since fetch_array() is being used. fetch_array() will also fill numeric indexes, true, and that would be just as dangerous as using the original expression as an index. A clear, descriptive alias is the best option, you can modifiy the query as much as you want; as long as the data comes out under the correct alias the rest of the code doesn't care. Quote Link to comment 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.