Page 1 of 1

mysqli fetch_assoc double results

Posted: Thu Aug 05, 2010 10:11 am
by seezee
I'm getting double results with this (error checking removed & output simplified for clarity):

Code: Select all

<?php $link = mysqli_connect('p:localhost', 'login', 'password');
$db = mysqli_select_db($link, 'dbname');
$takebake = mysqli_query($link, 'SELECT id, name FROM pages WHERE id > 200 AND id < 401 ORDER BY name');
while ($row = mysqli_fetch_assoc($takebake)) {
    $meals[] = $row;
    } 
?>
<?php foreach ($meals as $name): ?>
<?php echo '<li><a href="?page=meal'.$name['id'].'" name="meal['.$name['id'].']" title="'.$name['name'].'">'.$name['name'].'</a></li>
<li class="portions">
  <input id="mealSP-'.$name['id'].'" name="mealSP['.$name['id'].']" />
  <label for="mealSP-'.$name['id'].'">Single Portion</label>
</li>
<li class="portions">
  <input id="mealFP-'.$name['id'].'" name="mealFP['.$name['id'].']" />
  <label for="mealFP-'.$name['id'].'">Family Pack</label>
</li>'; ?>
<?php endforeach ?>
Initially I attributed this to the fact that I was using mysqli_fetch_array, but changing that to fetch_assoc didn't solve the problem.

You can see the output here.

Thanks,

Re: mysqli fetch_assoc double results

Posted: Thu Aug 05, 2010 10:57 am
by JakeJ
You've got a for each loop inside of a while loop. $row is already an array, why are you assigning it to another array ($meals[])?

Forget the foreach loop and just use the while loop. But if you have more than one row, you should be using "fetch_array" which has both associate and numeric indexes.

Re: mysqli fetch_assoc double results

Posted: Thu Aug 05, 2010 11:16 am
by AbraCadaver
JakeJ wrote:Forget the foreach loop and just use the while loop. But if you have more than one row, you should be using "fetch_array" which has both associate and numeric indexes.
I agree with the first part, but why would you want a numeric and associative if you are only using associative?

Re: mysqli fetch_assoc double results

Posted: Thu Aug 05, 2010 11:22 am
by seezee
JakeJ wrote:You've got a for each loop inside of a while loop. $row is already an array, why are you assigning it to another array ($meals[])?
Because I'm a Dad-blasted newbie who doesn't know his a** from a hole in the ground.
Forget the foreach loop and just use the while loop.
Like this?

Code: Select all

<?php while ($rowTB = mysqli_fetch_assoc($takebake)) {
echo '<li><a href="?page=meal'.$rowTB['id'].'" name="meal['.$rowTB['id'].']" title="'.$rowTB['name'].'">'.$rowTB['name'].'</a></li>
<li>
  <input id="mealSP-'.$rowTB['id'].'" name="mealSP['.$rowTB['id'].']" />
  <label for="mealSP-'.$rowTB['id'].'">Single Portion</label>
</li>
<li>
  <input id="mealFP-'.$rowTB['id'].'" name="mealFP['.$rowTB['id'].']" />
  <label for="mealFP-'.$rowTB['id'].'">Family Pack</label>
</li>';
}
?>
Hooray! It works!
But if you have more than one row, you should be using "fetch_array" which has both associate and numeric indexes.
It's my understanding that fetch_array without the second parameter will yield double results, also, and that fetch_assoc is the same as fetch_array with the MYSQL_ASSOC parameter.

Thanks for the help. I've been wrestling with this for a day & a half.

Re: mysqli fetch_assoc double results

Posted: Thu Aug 05, 2010 12:38 pm
by JakeJ
No, fetch_array doesn't produce double results, I just wanted to point out that it gives you numeric and associate keys to the same data set.

And I'm glad it's working.

And Abracadaver, I was just pointing out the nature of fetch_array; not suggesting he use numeric and associative keys both.