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 Link to comment https://forums.phpfreaks.com/topic/75321-solved-adding-something-to-the-database-with-an-id/ 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 Link to comment https://forums.phpfreaks.com/topic/75321-solved-adding-something-to-the-database-with-an-id/#findComment-380940 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)? Link to comment https://forums.phpfreaks.com/topic/75321-solved-adding-something-to-the-database-with-an-id/#findComment-380945 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 Link to comment https://forums.phpfreaks.com/topic/75321-solved-adding-something-to-the-database-with-an-id/#findComment-380950 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; Link to comment https://forums.phpfreaks.com/topic/75321-solved-adding-something-to-the-database-with-an-id/#findComment-380951 Share on other sites More sharing options...
jonoc33 Posted October 30, 2007 Author Share Posted October 30, 2007 Works. Thanks alot Link to comment https://forums.phpfreaks.com/topic/75321-solved-adding-something-to-the-database-with-an-id/#findComment-380953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.