jonoc33 Posted October 30, 2007 Share Posted October 30, 2007 Hi guys I have a message of the day system where the admin submits a message of the day and adds it to the database. When you add to the database I want to give it an ID that goes 0-1-2-3-4 etc. when you add it. <?php include("config.php"); $con = mysql_connect("localhost","jonoc33","*****"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("revolutiongamerz_net_-_squads", $con); $sql="INSERT INTO alpha_motd (motd) VALUES ('" . $_POST["motd"] . "')"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br>" . mysql_error); echo "<center>Message of the Day Added."; echo "<center><a href=admin.php>Back</a>"; ?> That is the code which adds it to the database. Notice it didnt add anything to ID, which I need someone to fix up for me. Then, I have a php script which shows what the message of the day is. But what I need it to do is show the most recent one, so the higher the ID the more recent it is. How would I do that? The script which shows what the FIRST record in the database is is this: <? $results = mysql_query("SELECT motd FROM alpha_motd"); if($values = mysql_fetch_array($results)){ echo $values['motd']; } ?> Can someone fix up both for me? Thanks Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted October 30, 2007 Share Posted October 30, 2007 Do you have Id field in the table, if you dont have you can add one and make it auto increment so when you add new records the id will be assigned automatically `id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY later you can fetch Ascending or descending order Quote Link to comment Share on other sites More sharing options...
jonoc33 Posted October 30, 2007 Author Share Posted October 30, 2007 n~ link=topic=165647.msg728446#msg728446 date=1193725858] Do you have Id field in the table, if you dont have you can add one and make it auto increment so when you add new records the id will be assigned automatically `id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY later you can fetch Ascending or descending order ID is working now, how would I fetch the first record in Descending order (the one with the highest id)? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted October 30, 2007 Share Posted October 30, 2007 SELECT * FROM `table_name` ORDER BY ID DESC; will fetch all data in descending order SELECT * FROM `table_name` ORDER BY ID DESC LIMIT 1; will fetch only 1 record Quote Link to comment Share on other sites More sharing options...
kopytko Posted October 30, 2007 Share Posted October 30, 2007 If you want to get new ID, that was inserted by your insert query, you can use mysql_insert_id function. If you want to get highest ID in your table, then use this query: select max(ID) as max_id from alpha_motd; Quote Link to comment Share on other sites More sharing options...
jonoc33 Posted October 30, 2007 Author Share Posted October 30, 2007 Works. Thanks alot 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.