php_phreak_91 Posted June 20, 2007 Share Posted June 20, 2007 Hi, I'm having some trouble looping through the records of a MySQL table using PEAR DB. Here is my code: $navigationSql = "SELECT * FROM nav"; $navigation =& $db->query($navigationSql); while ($navigation->fetchInto($nav)){ define("NAVIGATION", "<a href=\"$nav[2]\" target=\"$nav[3]\" title=\"$nav[1]\">$nav[1]</a><br />"); } The when I enter: print NAVIGATION; into any of my PHP scripts it should loop through the stuff in table named nav, but it is only displaying the first record and so far everything I've tried does not seem to make it loop through all of the records in the table and I can't figure out why not ??? Thanks in advance for your help. Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/ Share on other sites More sharing options...
trq Posted June 20, 2007 Share Posted June 20, 2007 Take a look at the manual entry for define. Define creates constants, and constants cannot be changed. You would need something like.... <?php while ($navigation->fetchInto($nav)){ $navigation .= "<a href=\"$nav[2]\" target=\"$nav[3]\" title=\"$nav[1]\">$nav[1]</a><br />"; } echo $navigation; ?> Instead. Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278918 Share on other sites More sharing options...
php_phreak_91 Posted June 20, 2007 Author Share Posted June 20, 2007 OK thanks for your reply. I tried what you said and now it spits an error out at me: Catchable fatal error: Object of class DB_result could not be converted to string Any help with what it means please ??? Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278921 Share on other sites More sharing options...
trq Posted June 21, 2007 Share Posted June 21, 2007 Ive never used said db class, but this looks more logical. while ($nav = $navigation->fetchInto($nav)) { Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278924 Share on other sites More sharing options...
php_phreak_91 Posted June 21, 2007 Author Share Posted June 21, 2007 Ive never used said db class, but this looks more logical. while ($nav = $navigation->fetchInto($nav)) { Nope that doesn't work either... Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278926 Share on other sites More sharing options...
corbin Posted June 21, 2007 Share Posted June 21, 2007 To build on to thorpe's post, try doing: while($n = $navigaton->fetchInto($nav)) { print_r($n); } I'm not sure how pear DB works though so I don't know for sure what datatype this method is returning.... Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278929 Share on other sites More sharing options...
Corona4456 Posted June 21, 2007 Share Posted June 21, 2007 <?php $navigationSql = "SELECT * FROM nav"; $navResult =& $db->query($navigationSql); while ($navResult->fetchInto($nav)){ $navigation .= "<a href=\"$nav[2]\" target=\"$nav[3]\" title=\"$nav[1]\">$nav[1]</a><br />"; } echo $navigation; ?> Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278931 Share on other sites More sharing options...
corbin Posted June 21, 2007 Share Posted June 21, 2007 Ohhhh so fetchInfo([variable]) is taking the variable by reference and altering it? Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278933 Share on other sites More sharing options...
Corona4456 Posted June 21, 2007 Share Posted June 21, 2007 Ohhhh so fetchInfo([variable]) is taking the variable by reference and altering it? Yes. According to the documentation: http://pear.php.net/package/DB/docs/1.7.11/DB/DB_result.html#methodfetchInto Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278934 Share on other sites More sharing options...
php_phreak_91 Posted June 21, 2007 Author Share Posted June 21, 2007 <?php $navigationSql = "SELECT * FROM nav"; $navResult =& $db->query($navigationSql); while ($navResult->fetchInto($nav)){ $navigation .= "<a href=\"$nav[2]\" target=\"$nav[3]\" title=\"$nav[1]\">$nav[1]</a><br />"; } echo $navigation; ?> Well it does not output any errors, but it doesn't seem to be looping through the records either... *EDIT* I found that in order for it to work you MUST use print_r like: print_r ($navigation); Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278937 Share on other sites More sharing options...
corbin Posted June 21, 2007 Share Posted June 21, 2007 Umm you shouldn't have to use print_r on navigation... It's a string, not an array >.<. Corona, read the documentation? Crazy talk ;p Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278940 Share on other sites More sharing options...
Corona4456 Posted June 21, 2007 Share Posted June 21, 2007 *EDIT* I found that in order for it to work you MUST use print_r like: print_r ($navigation); That's odd... the '.=' should not make it an array. Umm you shouldn't have to use print_r on navigation... It's a string, not an array >.<. Corona, read the documentation? Crazy talk ;p LOL Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278941 Share on other sites More sharing options...
php_phreak_91 Posted June 21, 2007 Author Share Posted June 21, 2007 Umm you shouldn't have to use print_r on navigation... It's a string, not an array >.<. Corona, read the documentation? Crazy talk ;p Well it doesn't matter now anyway as I figured it out, thanks to the very good help of you generous people. I even found that I could put $navigation into a constant with: define("NAVIGATION", $navigation); Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278942 Share on other sites More sharing options...
Corona4456 Posted June 21, 2007 Share Posted June 21, 2007 Heh... well glad to help Link to comment https://forums.phpfreaks.com/topic/56475-having-trouble-looping-through-records-in-a-mysql-table/#findComment-278944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.