samsbc12 Posted December 26, 2006 Share Posted December 26, 2006 basically what i have done is I have a latests news box on the side of my siteI can add an entry but I want to recall the last one entered and display the info<? include("dbinfo.inc.php"); $result = mysql_query("SELECT FROM LatestNews WHERE ID = MAX(ID);") //Pulls contact submissions or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<h2> "; echo $row['date']; echo "</h2>"; echo "<p>"; echo $row['message']; echo "</p>"; } mysql_close();?>Gives me You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM LatestNews WHERE ID = MAX(ID)' at line 1 I have no idea what i am doing I am using sql version Client API version 4.0.27 Aslo a side note when ever i echo a variable in a double qouted string i get errors any ideas Quote Link to comment Share on other sites More sharing options...
btherl Posted December 26, 2006 Share Posted December 26, 2006 Here are two ways to do it:[code=php:0]SELECT * FROM LatestNews ORDER BY ID DESC LIMIT 1[/code][code=php:0]SELECT * FROM LatestNews WHERE ID = (SELECT MAX(ID) FROM LatestNews)[/code]The first one is probably faster. The reason your query doesn't work (apart from missing the "*" after select) is that you cannot use max() as a condition unless you are also using group by. You can use max() as the result of the query anytime, but not as a condition. Quote Link to comment Share on other sites More sharing options...
fenway Posted December 26, 2006 Share Posted December 26, 2006 Just remember that it probably makes more sense to use a datetime field for this query... 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.