Value of last row of query??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Value of last row of query??

Post by mattcooper »

Hi all,

I'm running the following query:

Code: Select all

$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.

What am I doing wrong?

Thanks in advance!
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

You already have a value for $num, so $last = $row[$num]; won't work, and is also wrong.
It would be $last = $row['num'];
Anyway:

Code: Select all

$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 

 
$new = $num+1;
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

What I was trying to do was get the value of the page_id at row "$num", not $num itself. $num+1 would give me the number of rows +1, which I'm not interested in. What I want is the number stored in the last row (row "$num")...

Thanks for your help.
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

But $num is only the numer of rows.
If you want to have a value from a row, you have to use $row['bla'], bla - the colum you are looking at.
With $num you have the total rows, so you know which is the last row.
Then only do a query on the last row.
harsh789
Forum Newbie
Posts: 4
Joined: Fri Jun 09, 2006 12:11 pm

Post by harsh789 »

The last row will be $num-1 since rowcount starts from 0.

Moreover the batter way is,

Code: Select all

$sql3 = "SELECT max(page_id) page_id FROM $table"; 
$result3 = mysql_query($sql3) or die ("Couldn't perform query -".mysql_error()); 
$row = mysql_fetch_array($result3); 

// Now add 1 to the max value to get next value
$new = $row['page_id ']+1;
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Am I mistaken in thinking that asking for $row['$num'] (e.g. $row['16']) should return the value of page_id at row 16?

If so, how do I use $num to perform the query as you recommend? I wasn't aware that you could query rows where you didn't know their value. In this case, the number contained in page_id will not necessarily be the same as $num.

Any thoughts?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

If I get what your asking right. just change page_id to num
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

But that would result in a query like this:

Code: Select all

$sql = "SELECT $num FROM $table";
...which makes no sense to me. It doesn't relate to anything; it's the same as saying "SELECT 16 FROM $table"; - do you see what I mean?

There must be an alternatie way to do this - can you use the MySQL query to select JUST the last row of $table?

Thanks again.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

If you only want the last row's ID

Code: Select all

$result = mysql_query("SELECT MAX(NUM) AS LAST_NUM FROM table");
$result = mysql_fetch_array($result);
echo "Last Page#" . $result[LAST_NUM];
Post Reply