Jump to content

mysql_num_rows Question


avo

Recommended Posts

Hi All

Any ideas please why i can not get the row count from this

if i put a number in <= i can pull the rows up to this number
but with this code it allways 0
[quote]$result="SELECT units_covered FROM office_1 WHERE customer_name='".$_POST['drp_customers']."' ORDER BY units_covered ASC";
$query=mysql_query ($result) or die (mysql_error());
$row_count=mysql_num_rows(query) ;
while ($row=mysql_fetch_array ($query))
{
for($i=0;$i<=$row_count ;$i++)
{
$r=$row['units_covered'];
$v=explode("'", $r);
$units="<option>";
$units.="$v[$i]";
$units.="</option>";
echo $units ;
}
}[/quote]

thanks in advance
Link to comment
Share on other sites

Also I'd chnage this:
[code]while ($row=mysql_fetch_array ($query))
{
  for($i=0;$i<=$row_count ;$i++)
  {
      $r=$row['units_covered'];
      $v=explode("'", $r);
      $units="<option>";
      $units.="$v[$i]";
      $units.="</option>";
      echo $units ;
  }
}[/code]

to the following:
[code]$i = 0;
while ($row = mysql_fetch_array ($query))
{
      $r = $row['units_covered'];
      $v = explode("'", $r);
      $units  = "<option>";
      $units .= $v[$i];
      $units .= "</option>";

      echo $units;

      $i++;
}[/code]
Link to comment
Share on other sites

HI thanks

Would there be a better way to do this as i need to count the explode to echo the results
as all the values are stores in on coloum in one row


[quote]while ($row=mysql_fetch_array ($query))
{
for($i=0;$i<=$count ;$i++)
{
$r=$row['units_covered'];
$v=explode("'", $r);
$count=count($v);
$units="<option>";
$units.="$v[$i]";
$units.="</option>";
echo $units ;
}
}[/quote]

my first post was wrong i did not need to count the rows but the explode
thanks
Link to comment
Share on other sites

That is what the new code provided does:
[code]$i = 0;
while ($row = mysql_fetch_array ($query))
{
      $r = $row['units_covered'];
      $v = explode("'", $r);
      $units  = "<option>";
      $units .= $v[$i];
      $units .= "</option>";

      echo $units;

      $i++;
}[/code]
The while loop automatically calculates how many times it needs to loop in order to get all the results. Also the variable $i automatically increases by 1 with [i]$i++[/i], So when $v[$i] is stated when it goes for the first loop it use $v[0] then it use $v[1] for the secound loop, $v[2] for the third loop etc.

Bascially the while loop and the for loop was doiing the same thing, but in a different way. You code should still ruin fine.
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.