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.

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

<?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
Share on other sites

*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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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