By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

Fetching

P: n/a
how to fetch the last five records in mysql through php

May 22 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 5月22日, 下午3时42分, java.i...@gmail.com wrote:
how to fetch the last five records in mysql through php
1. save all query results in array, and use array_slice() get last
five records.
[php]
$a = range(0,100);
var_dump( array_slice($a, -5));

2. modify u SQL cmd "ORDER BY DESC limit 5".

so sorry , i can't express myself very well in english.

May 22 '07 #2

P: n/a
Message-ID: <11**********************@36g2000prm.googlegroups. comfrom
ja*******@gmail.com contained the following:
>how to fetch the last five records in mysql through php
Define 'last'.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 22 '07 #3

P: n/a
On 22 Mai, 09:42, java.i...@gmail.com wrote:
how to fetch the last five records in mysql through php
You can use mysql_data_seek to move the internal row pointer to the
desired position. After then using a mysql_fetch_*() function in a
while loop will iterate until the last position of the results.
Example:
[snip]
if (!$result = mysql_query('SELECT * WHERE 1=1')) {
die('Invalid query: ' . mysql_error());
}

if ($num_rows = mysql_num_rows($result)) {
mysql_data_seek($result, $num_rows-5);
while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
}
var_dump($data);
[/snap]

purcaholic

May 22 '07 #4

P: n/a
On 22.05.2007 09:42 ja*******@gmail.com wrote:
how to fetch the last five records in mysql through php
The words "first" and "last" only make sense when you tell mysql how to
sort the records, and if you did, just replace ASC with DESC and you're
in business ;)
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 22 '07 #5

P: n/a
On 5月22日, 下午3时42分, java.i...@gmail.com wrote:
how to fetch the last five records in mysql through php
1. save all query results in array, and use array_slice() get last
five records.
[php]
$a = range(0,100);
var_dump( array_slice($a, -5));

2. modify u SQL cmd "ORDER BY DESC limit 5".

so sorry , i can't express myself very well in english.

May 22 '07 #6

P: n/a
java.inet wrote:
how to fetch the last five records in mysql through php
Don't. Modify your SQL to only select the last five records.

SELECT x.*
FROM (SELECT *
FROM table
ORDER BY sortorder DESC
LIMIT 5) x
ORDER BY x.sortorder

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 22 '07 #7

P: n/a
On May 22, 4:04 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
java.inet wrote:
how to fetch the last five records in mysql through php

Don't. Modify your SQL to only select the last five records.

SELECT x.*
FROM (SELECT *
FROM table
ORDER BY sortorder DESC
LIMIT 5) x
ORDER BY x.sortorder

--
Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
completely agree with Toby. I used the exact method when designing a
module that displayed a summary of the 5 most recent help desk tickets
on our network management home page. always try to query only what
you need from a database, anything more is just unnecessary overhead.

May 22 '07 #8

P: n/a
On May 22, 4:04 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
java.inet wrote:
how to fetch the last five records in mysql through php

Don't. Modify your SQL to only select the last five records.

SELECT x.*
FROM (SELECT *
FROM table
ORDER BY sortorder DESC
LIMIT 5) x
ORDER BY x.sortorder

--
Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
completely agree with Toby. I used the exact method when designing a
module that displayed a summary of the 5 most recent help desk tickets
on our network management home page. always try to query only what
you need from a database, anything more is just unnecessary overhead.

May 22 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.