Warning: mysqli_num_rows() expects exactly 1 param

2019-03-07 00:52发布

问题:

i have code like this in mysql_query , works fine. but i moving all the code to mysqli_ its throw error like in the title

mysql

$count = mysql_query("SELECT COUNT(*) FROM xxx limit 2") or die(mysql_error());
      $count = mysql_result($count,0);
      for($i=0; $i<$count;$i++){
        echo '<li data-target="#transition-timer-carousel" data-slide-to="'.$i.'"'; if($i==0){ echo 'class="active"'; } echo '></li>';
      }

mysqli

   $count = mysqli_query($con,"SELECT COUNT(*) FROM xxx limit 2") or die(mysqli_error());
      $count = mysqli_num_rows($count,0);
      for($i=0; $i<$count;$i++){
        echo '<li data-target="#transition-timer-carousel" data-slide-to="'.$i.'"'; if($i==0){ echo 'class="active"'; } echo '></li>';
      }

pls help .. editt. this code for

work : http://www.imagebam.com/image/830cf2469802470 in mysql_

not working : i already did the mysqli_num_rows ($count); http://www.imagebam.com/image/a32c87469802459

this code for counting this : http://www.imagebam.com/image/f8a0b9469803871 see the red

回答1:

mysqli_num_rows does nothing even remotely similar to mysql_result.

The replacement for mysql_result in mysqli in this case would be to fetch the entire row and use the first element only, something like;

$result = mysql_query("SELECT COUNT(*) FROM xxx limit 2") or die(mysql_error());
$row = mysqli_fetch_row($result);
$count = $row[0];

for($i=0; $i<$count;$i++){
    echo '<li data-target="#transition-timer-carousel" data-slide-to="'.$i.'"'; if($i==0){ echo 'class="active"'; } echo '></li>';
}


回答2:

According to PHP Manual

you must change $count = mysqli_num_rows($count,0); into $count = mysqli_num_rows($count);

NOTE:Don't use mysql any more.This extension was deprecated in PHP 5.5.0



回答3:

this error throws because PHP function

 int mysqli_num_rows ( mysqli_result $result )

needs only one argument.

Here is http://php.net/manual/ru/mysqli-result.num-rows.php documentation



回答4:

correct way of doing is

$sql="SELECT COUNT(*) FROM xxx limit 2";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  printf("Result set has %d rows.\n",$rowcount);
  // Free result set
  mysqli_free_result($result);
  }

mysqli_close($con);


标签: php mysql mysqli