473,394 Members | 1,759 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,394 software developers and data experts.

Fetching

how to fetch the last five records in mysql through php

May 22 '07 #1
8 1325
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andres Baravalle | last post by:
Hi, I have a problem using a URL fetching algorithm (HTTP 1.0). To download an image, I used to use this algorithm: <?php Header("Content-type: image/gif"); $referer =...
1
by: Srinivasan R | last post by:
Hi, I am trying to get asynchronous fetching of data from sql server database. I would like to show the progress of fetching records in GUI. Is there any simple/complex steps for the same in C#...
0
by: Shujun Huang | last post by:
Hi, I am working on converting Informix database to Postgre. I have one question for fetching records using PostgreSQL. The record I am fetching is a variable size text string. Before fetching...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
1
by: asenthil | last post by:
Hai, i had tried to write a string which is fetched from a database into a file.... Here are my codings to write that fetched string into a text file... HANDLE hFile; DWORD wmWritten;...
0
by: satishr23 | last post by:
Hi, I am trying to parse the contents of a PHP page from the web which requires me to authenticate. I guess the way to do this is to make my program login to the login page,grab the cookie and...
5
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
2
by: SunshineInTheRain | last post by:
I'm trying to modify a long long code within a button click by make the insert/update/delete/select using the same transaction. Purpose is to make sure every operation can be rollback instead of some...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.