Jump to content

Having trouble looping through records in a MySQL table


php_phreak_91

Recommended Posts

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.

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.

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 ???

<?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;

?>

<?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); 

*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 :)

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); 

 

;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.