473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Grabing the 5 latest rows

I am trying to create a PHP script that queries entries from a MySQL
table:

<?php
$user="myusername";
$password="mypassword";
$database="mydatabase";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT ititle, inumber FROM nucleus_item";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['ititle']} <br>" .
"Subject : {$row['inumber']} <br>";
}
mysql_close();
?>

What I'd like to do is to grab the 5 latest rows of the "inumber"
field. This field starts from 1 and currently ends at 2100 but everyday
there are new entries. So I think I should sort the table rows from the
greatest number to the lowest, and take only the 5 latest entries. I
didn't find on php.net how to do that. Any idea?
Thanks,

Lenard.

Dec 13 '06 #1
3 1209

Lenard Redwood wrote:
I am trying to create a PHP script that queries entries from a MySQL
table:

<?php
$user="myusername";
$password="mypassword";
$database="mydatabase";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT ititle, inumber FROM nucleus_item";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['ititle']} <br>" .
"Subject : {$row['inumber']} <br>";
}
mysql_close();
?>

What I'd like to do is to grab the 5 latest rows of the "inumber"
field. This field starts from 1 and currently ends at 2100 but everyday
there are new entries. So I think I should sort the table rows from the
greatest number to the lowest, and take only the 5 latest entries. I
didn't find on php.net how to do that. Any idea?
Thanks,

Lenard.
Be sure to set what field to order by, what order (DESC/ASC), and to
limit it to five, use LIMIT=5

Good luck!

Dec 13 '06 #2
Lenard Redwood wrote:
I am trying to create a PHP script that queries entries from a MySQL
table:

<?php
$user="myusername";
$password="mypassword";
$database="mydatabase";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT ititle, inumber FROM nucleus_item";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['ititle']} <br>" .
"Subject : {$row['inumber']} <br>";
}
mysql_close();
?>

What I'd like to do is to grab the 5 latest rows of the "inumber"
field. This field starts from 1 and currently ends at 2100 but everyday
there are new entries. So I think I should sort the table rows from the
greatest number to the lowest, and take only the 5 latest entries. I
didn't find on php.net how to do that. Any idea?
Hi,

You don't find it at php.net because you should have searched your mysql
documentation. :-)
The keyword you are looking for is LIMIT

example:
SELECT firstname, lastname FROM tblusers ORDER BY lastname DESC LIMIT 10

Regards,
Erwin Moller

Thanks,

Lenard.
Dec 13 '06 #3

Erwin Moller wrote:
SELECT firstname, lastname FROM tblusers ORDER BY lastname DESC LIMIT 10
Thanks, it's so simple... Problem is finding where in the docs... But
this worked very well, thanks.

Dec 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
2
by: hunglm | last post by:
Hi all, I'm trying to get the latest URL that a user visited in the frame. But it just doesn't work. function setURL() { //document.cc.fm.URL.value=document.main.location.href;...
4
by: Taxi Driver | last post by:
Does anyone know of a good script to gather email addresses from a specific web site. Thank you.
1
by: Ed L. | last post by:
I'm trying to unsuccessfully to drop a database using the latest CVS code for 8.0.0, getting the following error: ERROR: cannot drop the currently open database So I must have a connection...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
4
by: TGEAR | last post by:
Itemlookup table Field names : index_id (primary key), itemno, description. It has a child table, which is ItemPriceHistory table The relationship to the child table is one (parent table)-to-many...
7
by: Eugene | last post by:
Hi all, I have the following table Name Date Wish Valid Name is person's name, date defaults to getdate() and is never assigned directly (datetime field), Wish is some message, and...
1
by: crimanysakes | last post by:
Someone should write a FAQ on how to grab various "latest dates" from MySQL; cuz I'm quite confused. Not sure how much to include; but here' my query to get my first "subset" of data: select...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.