Jump to content

Archived

This topic is now archived and is closed to further replies.

stone.cold.steve.austin

printing last result of recordset

Recommended Posts

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,
Richard


Share this post


Link to post
Share on other sites
if you only need the first and last row, you can find out how many items are in the array using count(), and go from there:
[code]
$rsfeed = mysql_query("select Title from Abstracts where (ID = '12'));
$thearray = mysql_fetch_row($rsfeed);

echo $thearray[0];
$count = count($thearray);
echo $thearray[$count - 1];
[/code]

notice that the last element is "$count - 1". this is due to the fact of indexes on arrays starting with 0, not 1.

hope this helps.

also, if you'd like to simply see the structure of your array (with indexes and all), you can do the following:
[code]
echo "<pre>\n";
print_r($thearray);
echo "</pre>\n";
[/code]

Share this post


Link to post
Share on other sites
Thanks for the help. but it doesn't seem to be working, the principle is correct but this bit is wrong i think:

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

the mysql_fetch_row only returns 1 row, ie. 1 title, yet there is actually multiple titles for ID=12, i need an array of these titles, i have tried using instead:

$thearray = mysql_fetch_array($rsfeed);

but that doesn't work, what command should i be using to get my resultset into an array?

thanks,
richard

Share this post


Link to post
Share on other sites
Thanks for the help. but it doesn't seem to be working, the principle is correct but this bit is wrong i think:

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

the mysql_fetch_row only returns 1 row, ie. 1 title, yet there is actually multiple titles for ID=12, i need an array of these titles, i have tried using instead:

$thearray = mysql_fetch_array($rsfeed);

but that doesn't work, what command should i be using to get my resultset into an array?

thanks,
richard

Share this post


Link to post
Share on other sites
Thanks for the help. but it doesn't seem to be working, the principle is correct but this bit is wrong i think:

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

the mysql_fetch_row only returns 1 row, ie. 1 title, yet there is actually multiple titles for ID=12, i need an array of these titles, i have tried using instead:

$thearray = mysql_fetch_array($rsfeed);

but that doesn't work, what command should i be using to get my resultset into an array?

thanks,
richard

Share this post


Link to post
Share on other sites
You need to place the fetches in a loop. Each one returns one row.
[code]<?php
$q = "select Title from Abstracts where (ID = '12')";
$rsfeed = mysql_query($q) or die('Problem with query: ' . $q . '<br />' . mysql_error());
$num = mysql_num_rows($rsfeed) - 1;
$w = 0;
while($rw = mysql_fetch_assoc($rsfeed)) {
    if ($w == 0 || $w == $num) echo $rw['Title'] . '<br />';
    $w++;
}
?>[/code]

This code should loop through the rows and print the first and the last "Title".

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.