Jump to content

Archived

This topic is now archived and is closed to further replies.

avo

mysql_num_rows Question

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

Share this post


Link to post
Share on other sites
[quote]$row_count=mysql_num_rows(query) ;[/quote]

replace the above line with
[quote]
$row_count=mysql_num_rows($query) ;[/quote]

Thank you,
Karthi Keyan.

Share this post


Link to post
Share on other sites
HI Thanks

Now thats 2 stuppid questions i have asked today i must be doing to much php SLEEPY EYES

thanks agian.

Share this post


Link to post
Share on other sites
ok np... refer php.net for syntax... once you have Zend studio then it will show you syntax errors during the time of coding

Thank you,
Karthi Keyan.

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.