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