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. Quote Link to comment 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. Quote Link to comment 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 ??? Quote Link to comment 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)) { Quote Link to comment 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... Quote Link to comment 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.... Quote Link to comment 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; ?> Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted June 21, 2007 Share Posted June 21, 2007 Heh... well glad to help Quote Link to comment 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.