Hi,

I have the following peice of code which pulls data from a database:

$rsfeed = mysql_query("select Title from Abstracts where (ID = '12'));
$thearray = mysql_fetch_row($rsfeed);

I can print the title using: print $thearray[0]; but theactual resultset is more then 1 row, and what i need to do is print the first row ($thearray[0]) and the last row, how do i modify the above code to achieve that?

thanks

Hi,

I have the following peice of code which pulls data from a database:

$rsfeed = mysql_query("select Title from Abstracts where (ID = '12'));
$thearray = mysql_fetch_row($rsfeed);

I can print the title using:
print $thearray[0];

but theactual resultset is more then 1 row, and what i need to do is print the first row ($thearray[0]) and the last row, how do i modify the above code to achieve that?

thanks

First off:
$rsfeed = mysql_query("select Title from Abstracts where (ID = '12'));
you dont need a parantesise (ID = '12')

just use:
$rsfeed = mysql_query("select Title from Abstracts where ID = '12');

Second, if the ID is unique in your table Abstract, then you will only get 1 row anyway.

If ID is not unique, then your will get many rows that have the same ID = 12
in which case you would reference the first row as $rsfeed[0]
and the last row as end($rsfeed);

print $thearray[0]; //first
print $thearray[count($thearray)-1]; //last
// because the index starts with zero
print $thearray[0]; //first
print $thearray[count($thearray)-1]; //last
// because the index starts with zero

In php the easier way to get the last element is to use end($arr)

Comments
Thanks, much simpler
Class

In php the easier way to get the last element is to use end($arr)

Thank you, going to edit some scripts

In php the easier way to get the last element is to use end($arr)

Nice one uncle, didn't know about that one. That's REALLY useful.

Why not just use sql to do it for you? Say you want the last (highest id) of a set ordered by id:
SELECT * FROM `table` ORDER BY id DESC LIMIT 1
That way you only get the one row where the ID is highest.
Et voila

This article has been dead for over six months. Start a new discussion instead.