Jump to content

Recommended Posts

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.

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.