Jump to content

while() problems


maxxtraxx

Recommended Posts

total newbie question here, so save your jeers until after you solve my prob.  ;D

 

i have a table of products that i just want to loop over using a while()  yet i only get one row.  here's what i'm doing.

 

<?php

include("includes/connx.php"); 

 

$url = $_SERVER['QUERY_STRING'];

$sql = 'SELECT * FROM products p, productGroups g WHERE p.group_id = g.group_id AND p.'.$url ;

$groups = mysql_query($sql, $dbh) or die (mysql_error());

$row_groups = mysql_fetch_assoc($groups);

?>

 

<?php

while($row = mysql_fetch_array($groups)){

echo ($row['name'] . "<br />");

  }

?>

i think it might be the mysql_fetch_assoc() that is limiting it, but i'm not really sure what that does to be honest...  as you can see i'm just getting beyond the basics and its a mighty deep pool here.

 

any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/59396-while-problems/
Share on other sites

If you're only getting one row then check your MySQL query and make sure it should be reporting more - check in cPanel if you're running online or have it installed on your PC.

 

As for the line suma has pointed out - remove it. If your query is only returning two results then having that extra line will be "disabling" the first row and you'll be seeing the second row onwards.

Link to comment
https://forums.phpfreaks.com/topic/59396-while-problems/#findComment-295084
Share on other sites

<?php
  include("includes/connx.php"); 
  $url = $_SERVER['QUERY_STRING'];
  $sql = 'SELECT * FROM products p, productGroups g WHERE p.group_id = g.group_id AND p.'.$url ;
  $groups = mysql_query($sql, $dbh) or die (mysql_error());
  $row_groups = mysql_fetch_assoc($groups);
  while ($row = mysql_fetch_array($groups)) {
    echo $row['name'] . "<br />";
  }
?>

 

I've removed the line for you. If you post your code between [code] and [/code] tags it makes it a lot easier to read.

Link to comment
https://forums.phpfreaks.com/topic/59396-while-problems/#findComment-295105
Share on other sites

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.