codlife Posted January 4, 2009 Share Posted January 4, 2009 Hi there, Pretty new to php so you'll have to bear with me if you don't mind! What i've got: sql table "news" with 2 field field 1: "user_id" - auto-inc integer field 2: "lnews" - text field What I'm trying to do: get my php include to display only the newest news item (highest incr. integer) My current non working code: <?php /*define variables*/ $con = mysql_connect("localhost", "root"); /*display error msg if cannot connect*/ if (!$con) { die("could not connect" . mysql_error()); } /*select database and connect to it*/ mysql_select_db("controlpanel", $con); /*display data from max user_id*/ $news_entry = mysql_query("SELECT MAX user_id FROM news"); while ($row = mysql_fetch_assoc($news_entry)) { $latestnews = $row['lnews']; echo $row["lnews"]; } /*write the entry here as this is the include for the site - apply css on the site*/ mysql_close($con); ?> Any help would be majorly appreciated as have been staring at this for hours! Link to comment https://forums.phpfreaks.com/topic/139424-help-please-how-do-i-use-select-max-properly/ Share on other sites More sharing options...
ohdang888 Posted January 4, 2009 Share Posted January 4, 2009 this is not a select max, its a ORBDER BY problem also, the way your table is set up is pretty weird, why would you order by the User id?...create more fields, like "News_id" $res = mysql_query("SELECT * FROM news ORDER BY user_id DESC LIMIT 5")or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/139424-help-please-how-do-i-use-select-max-properly/#findComment-729361 Share on other sites More sharing options...
priti Posted January 4, 2009 Share Posted January 4, 2009 I think you will receive some error when you run the query in query brwser SELECT MAX user_id FROM news As MAX is the function MAX(user_id) ......please go through the following link http://www.tizag.com/mysqlTutorial/mysqlmax.php Link to comment https://forums.phpfreaks.com/topic/139424-help-please-how-do-i-use-select-max-properly/#findComment-729364 Share on other sites More sharing options...
codlife Posted January 5, 2009 Author Share Posted January 5, 2009 Sorted - thank you kindly for your help! Link to comment https://forums.phpfreaks.com/topic/139424-help-please-how-do-i-use-select-max-properly/#findComment-729801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.