Page 1 of 1
Value of last row of query??
Posted: Mon Jun 12, 2006 9:40 am
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!
Posted: Mon Jun 12, 2006 9:46 am
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;
Posted: Mon Jun 12, 2006 9:50 am
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.
Posted: Mon Jun 12, 2006 9:54 am
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.
Posted: Mon Jun 12, 2006 9:58 am
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;
Posted: Mon Jun 12, 2006 9:59 am
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?
Posted: Mon Jun 12, 2006 10:33 am
by tecktalkcm0391
If I get what your asking right. just change page_id to num
Posted: Mon Jun 12, 2006 10:41 am
by mattcooper
But that would result in a query like this:
...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.
Posted: Mon Jun 12, 2006 10:53 am
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];