Jump to content


Photo

mysql_num_rows Question


  • Please log in to reply
6 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 July 2006 - 08:16 PM

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

$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 ;
}
}


thanks in advance
Im loving it ........

#2 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 02 July 2006 - 08:20 PM

$row_count=mysql_num_rows(query) ;


replace the above line with

$row_count=mysql_num_rows($query) ;


Thank you,
Karthi Keyan.
www.karthi.us

#3 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 July 2006 - 08:41 PM

HI Thanks

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

thanks agian.
Im loving it ........

#4 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 02 July 2006 - 08:58 PM

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.
www.karthi.us

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 July 2006 - 09:15 PM

Also I'd chnage this:
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 ;
   }
}

to the following:
$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++;
}


#6 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 July 2006 - 09:31 PM

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


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 ;
}
}


my first post was wrong i did not need to count the rows but the explode
thanks
Im loving it ........

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 July 2006 - 03:14 PM

That is what the new code provided does:
$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++;
}
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++, 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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users