Jump to content

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


Link to comment
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]
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.