stone.cold.steve.austin Posted March 7, 2006 Share Posted March 7, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/ Share on other sites More sharing options...
obsidian Posted March 7, 2006 Share Posted March 7, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15084 Share on other sites More sharing options...
stone.cold.steve.austin Posted March 8, 2006 Author Share Posted March 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15327 Share on other sites More sharing options...
stone.cold.steve.austin Posted March 8, 2006 Author Share Posted March 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15328 Share on other sites More sharing options...
stone.cold.steve.austin Posted March 8, 2006 Author Share Posted March 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15329 Share on other sites More sharing options...
stone.cold.steve.austin Posted March 8, 2006 Author Share Posted March 8, 2006 sorry the page didnt display and mutiple messages displayed for my post Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15330 Share on other sites More sharing options...
kenrbnsn Posted March 8, 2006 Share Posted March 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15410 Share on other sites More sharing options...
stone.cold.steve.austin Posted March 9, 2006 Author Share Posted March 9, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4337-printing-last-result-of-recordset/#findComment-15712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.