Taking information from a MySQL db using PHP
Started by Exc.BluePhoenix, Apr 05 2008 06:51 PM
6 replies to this topic
#1
Posted 05 April 2008 - 06:51 PM
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.
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.
#2
Posted 05 April 2008 - 07:24 PM
Check out mysql LIMIT. LIMIT 0, 4 will take the first row and 4 things from DB. Now if you want to get the last 4, then you can use ORDER BY as well. ORDER BY id DESC. It means, it takes 4,3,2,1. If you want to exclude 1. then u use ORDER BY id DESC LIMIT 0,3 This will give 4,3,2
Hope it helps.
mysql_query("SELECT * FROM ".$table." WHERE `something` = '$something' order by `id` DESC LIMIT 0,3");
Edited by Rainit, 05 April 2008 - 07:27 PM.
#3
Posted 05 April 2008 - 10:03 PM
Thanks for you reply.
Now to tell the truth, Im somewhat confuse by the query on the bottom.
First why $table? do I have to define it before the my $query?
Second, when you say 'something' = '$something' . Was i supposed to have a $something define somewhere?
Now Im big nub at this, I understand what you posted, but the var throw me off.
So with that in mind i search, and I got to this,
"SELECT title FROM lpcs ORDER BY id DESC LIMIT 3"
title is the field that I want to echo, id is the other field that I had, I want the first 3, and lpcs is the table. That is just a short version of yours, however, its easier for me because Im somewhat new at this.
However, what this echos is the following
Resource id #3
So my questions are, can you please explain a bit more where i need to define your vars? or can you please tell me why with the shorter version of your code its giving me Resource id #3 and not the title like i wanted? does it have to do with the way i make the MySQL tables?
Thanks you for your reply, and for the code, and Im sorry I dont understand it that well, Im just new at this.
Also here is the code Im using in the index page
Now to tell the truth, Im somewhat confuse by the query on the bottom.
First why $table? do I have to define it before the my $query?
Second, when you say 'something' = '$something' . Was i supposed to have a $something define somewhere?
Now Im big nub at this, I understand what you posted, but the var throw me off.
So with that in mind i search, and I got to this,
"SELECT title FROM lpcs ORDER BY id DESC LIMIT 3"
title is the field that I want to echo, id is the other field that I had, I want the first 3, and lpcs is the table. That is just a short version of yours, however, its easier for me because Im somewhat new at this.
However, what this echos is the following
Resource id #3
So my questions are, can you please explain a bit more where i need to define your vars? or can you please tell me why with the shorter version of your code its giving me Resource id #3 and not the title like i wanted? does it have to do with the way i make the MySQL tables?
Thanks you for your reply, and for the code, and Im sorry I dont understand it that well, Im just new at this.
Also here is the code Im using in the index page
/*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;
?>
Edited by Exc.BluePhoenix, 05 April 2008 - 10:06 PM.
#4
Posted 06 April 2008 - 12:12 AM
Exc.BluePhoenix, on Apr 5 2008, 11:03 PM, said:
First why $table? do I have to define it before the my $query?
The only thing that I would change is replacing "title" to "*" in your query, because you want to return all fields. So, your query would be:
$query="SELECT * FROM lpcs ORDER BY id DESC LIMIT 3";
#5
Posted 06 April 2008 - 03:34 AM
thanks for all your help guys, i got it good and working.
Thanks again.
Thanks again.
#6
Posted 06 April 2008 - 10:47 PM
Exc.BluePhoenix, on Apr 5 2008, 09:03 PM, said:
"SELECT title FROM lpcs ORDER BY id DESC LIMIT 3"
title is the field that I want to echo, id is the other field that I had, I want the first 3, and lpcs is the table. That is just a short version of yours, however, its easier for me because Im somewhat new at this.
title is the field that I want to echo, id is the other field that I had, I want the first 3, and lpcs is the table. That is just a short version of yours, however, its easier for me because Im somewhat new at this.
fycnrdths, on Apr 5 2008, 11:12 PM, said:
The only thing that I would change is replacing "title" to "*" in your query, because you want to return all fields. So, your query would be:
$query="SELECT * FROM lpcs ORDER BY id DESC LIMIT 3";
Exc.BluePhoenix, on Apr 6 2008, 02:34 AM, said:
thanks for all your help guys, i got it good and working.
Thanks again.
Thanks again.
#7
Posted 07 April 2008 - 11:10 AM
Okay sorry about that.
Yes, I used mysql_fetch_assoc.
Here is the code I used. thanks again for the help.
Yes, I used mysql_fetch_assoc.
Here is the code I used. thanks again for the help.
//Create query
$sql = "SELECT title FROM lpcs ORDER BY id DESC LIMIT 3";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
echo $row['title'].'<br />';
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
