mysqli fetch_assoc double results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

mysqli fetch_assoc double results

Post 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,
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: mysqli fetch_assoc double results

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mysqli fetch_assoc double results

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Re: mysqli fetch_assoc double results

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: mysqli fetch_assoc double results

Post 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.
Post Reply