Exc.BluePhoenix Posted April 5, 2008 Share Posted April 5, 2008 Hello, I have a very simple form made, with two fields id field and title field I made a php script that inserts the information put in the fields to a db. For example 1 for the id and the title South Park Movie. So lets say i put 3 more titles. What i want to do is to display this information in a part of my website, by taking the information from the db that i previously inserted the info in. However, i only want to take 3 of the 4 titles (since i only want to display the titles, the id is only there because i think it makes the extracting/taking process easier) I want to take the newest titles, so I would want to take title with id 2 3 4 but NOT 1, cause its now old. So my question is how can i do this? a MySQL query? I really dont have a clue on how to extract information from the db in blocks of 3 (in this case) and im hoping someone can show me how or direct me somewhere, to learn how to. If you dont understand my question please reply, and I will try my best to explain it better. Quote Link to comment https://forums.phpfreaks.com/topic/99764-solved-taking-information-from-a-database-mysql/ Share on other sites More sharing options...
quiettech Posted April 5, 2008 Share Posted April 5, 2008 Yes. It's through a SQL query using the SELECT statement and, on your case, conditioned by the LIMIT clause to just return 3 records. However without any information on the relevant tables structure it is impossible to answer your question. I do note however you have a very limited knowledge of SQL. That is perfectly fine. But it would would be in your best interest to search for SQL tutorials on the web. Only after you have acquired a minimum set of skills would it make sense, in my modest opinion, to post on these forums. Quote Link to comment https://forums.phpfreaks.com/topic/99764-solved-taking-information-from-a-database-mysql/#findComment-510255 Share on other sites More sharing options...
Barand Posted April 5, 2008 Share Posted April 5, 2008 You can limit the number of displayed records by using MySQL LIMIT clause, and as you want the latest first, use ORDER BY id DESC, which sorts the ids in descending sequence. So your query would be SELECT title FROM tablename ORDER BY id DESC LIMIT 3 Quote Link to comment https://forums.phpfreaks.com/topic/99764-solved-taking-information-from-a-database-mysql/#findComment-510258 Share on other sites More sharing options...
Exc.BluePhoenix Posted April 6, 2008 Author Share Posted April 6, 2008 Thanks for the replies. I tried the query SELECT title FROM tablename ORDER BY id DESC LIMIT 3 However, all it echo's is Resource id #3. I'm positive its because I must have made the table wrong. My table has two fields. Id field, and the title field. Here is the whole code I use <?php /*Connect to mysql server*/ $link=mysql_connect("localhost","ODBC","pass"); if(!$link) { die('Failed to connect to server, here is your error : ' . mysql_error()); } /*Select database*/ $db=mysql_select_db("lpcs1"); if(!$db) { die("Could not select db"); } //Create query $query="SELECT title FROM lpcs ORDER BY id DESC LIMIT 3"; $result=mysql_query($query); echo $result; ?> If you can please tell me what does the Resource id #3 result means? and if its the fact that my SQL table must be made in a certain way, or its my php code wrong? Thanks for your replies and earlier help. Quote Link to comment https://forums.phpfreaks.com/topic/99764-solved-taking-information-from-a-database-mysql/#findComment-510303 Share on other sites More sharing options...
Barand Posted April 6, 2008 Share Posted April 6, 2008 see FAQ http://www.phpfreaks.com/forums/index.php/topic,95441.0.html Quote Link to comment https://forums.phpfreaks.com/topic/99764-solved-taking-information-from-a-database-mysql/#findComment-510368 Share on other sites More sharing options...
Exc.BluePhoenix Posted April 6, 2008 Author Share Posted April 6, 2008 awesome, awesome... that helps a lot, I got working now, with no problems. Thanks a lot... Topic Solved. Quote Link to comment https://forums.phpfreaks.com/topic/99764-solved-taking-information-from-a-database-mysql/#findComment-510380 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.