Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Value of last row of query??
Old 06-12-2006, 11:36 AM Value of last row of query??
Skilled Talker

Posts: 82
Trades: 0
Hi all,
I'm running the following query:
PHP Code:
$sql3 "SELECT page_id FROM $table";
$result3 mysql_query($sql3) or die ("Couldn't perform query -".mysql_error());
$row mysql_fetch_array($result3);
$num mysql_num_rows($result3);
// Get the last number in the array of page_id's
$last $row[$num];
$new $last+1
... in order to generate a number for use later in the script. I was expecting "$row[$num]" to be the number stored in the last row of "page_id", but it's not: the only value I can get for $new is 1.

Please note that I am attempting to get the value contained in the last row of "page_id", not the row number itself!

What am I doing wrong?
Thanks in advance!
mattcooper is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-12-2006, 12:26 PM Re: Value of last row of query??
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
PHP Code:
$row mysql_fetch_array($result3); 
You'll only get an array of the first row if your do that. To get the last row, do:

PHP Code:
//the sql query
$sql3 "SELECT page_id FROM $table";

//preform the query
$result3 mysql_query($sql3) or die ("Couldn't perform query -".mysql_error());

//amount of rows in query
$num mysql_num_rows($result3);

//go to last row in query
mysql_data_seek($result3$num-1);

//fetch last row in query
$lastrow mysql_fetch_array($result3);

//print last page_id
echo $lastrow['page_id']; 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Old 06-12-2006, 01:08 PM Re: Value of last row of query??
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
If you only need one row, then you should not query the database for the entire table, it is very inefficient. In this case it looks as though you could just order by page_id in descending order, and the first row will be the row you are looking for:

Code:
$sql3 = "SELECT page_id FROM $table ORDER BY page_id DESC LIMIT 1";
$result3 = mysql_query($sql3) or die ("Couldn't perform query -".mysql_error()); 
$row = mysql_fetch_array($result3);
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 06-12-2006, 02:53 PM Re: Value of last row of query??
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
Provided that page_id is an "auto_increment" field. You won't have the right effect otherwise.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Old 06-13-2006, 05:23 AM Re: Value of last row of query??
Skilled Talker

Posts: 82
Trades: 0
thank you guys, both solid ideas that work.
mattcooper is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Value of last row of query??
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15124 seconds with 12 queries