Jump to content


Photo

printing last result of recordset


  • Please log in to reply
7 replies to this topic

#1 stone.cold.steve.austin

stone.cold.steve.austin
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 07 March 2006 - 02:56 PM

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




#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 March 2006 - 03:33 PM

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:
$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];

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:
echo "<pre>\n";
print_r($thearray);
echo "</pre>\n";

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 stone.cold.steve.austin

stone.cold.steve.austin
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 March 2006 - 09:19 AM

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

#4 stone.cold.steve.austin

stone.cold.steve.austin
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 March 2006 - 09:20 AM

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

#5 stone.cold.steve.austin

stone.cold.steve.austin
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 March 2006 - 09:23 AM

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

#6 stone.cold.steve.austin

stone.cold.steve.austin
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 March 2006 - 09:23 AM

sorry the page didnt display and mutiple messages displayed for my post

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 March 2006 - 03:06 PM

You need to place the fetches in a loop. Each one returns one row.
<?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++;
}
?>

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

Ken

#8 stone.cold.steve.austin

stone.cold.steve.austin
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 09 March 2006 - 08:50 AM

alright cheers for that, i was hoping there was a way to do it without the loop as the loop is very slow but oh well must be done :)

cheers.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users