【问题标题】:Warning: mysqli_num_rows() expects exactly 1 parameter, 2 given | mysql |mysqli [duplicate]警告:mysqli_num_rows() 只需要 1 个参数,给定 2 个 | mysql |mysqli [重复]
【发布时间】:2016-03-06 07:47:08
【问题描述】:

我在 mysql_query 中有这样的代码,工作正常。 但我将所有代码移动到 mysqli_ 它的抛出错误就像标题中一样

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

请帮助.. 编辑。 此代码用于​​p>

工作:http://www.imagebam.com/image/830cf2469802470 在 mysql_ 中

不工作:我已经做了 mysqli_num_rows ($count); http://www.imagebam.com/image/a32c87469802459

此计数代码:http://www.imagebam.com/image/f8a0b9469803871 见红色

【问题讨论】:

  • 它是阅读手册的更好方法,了解函数中发生了什么!
  • COUNT 将返回一个带有行数的整数,mysqli_num_rows(如果使用正确的参数调用)将计算返回的整数个数(一) ,而不是行数。可能不是你想要的。

标签: php mysql mysqli


【解决方案1】:

根据PHP Manual

您必须将$count = mysqli_num_rows($count,0); 更改为$count = mysqli_num_rows($count);

注意:不要再使用mysql了。这个扩展在PHP 5.5.0中被弃用了

【讨论】:

  • 我已经做 dat 请查看链接 mysql 工作 imagebam.com/image/830cf2469802470 ,不工作 mysqli imagebam.com/image/a32c87469802459
  • 您到底想要什么?用这个查询? SELECT COUNT(*) FROM xxx limit 2
  • 尝试编辑你的问题,不要放太多链接!
  • @RamaDiansyahS 如果您有图片或其他您认为有助于做出更好答案的事情,请尝试将它们添加到您的问题中。
【解决方案2】:

这个错误是因为 PHP 函数引发的

 int mysqli_num_rows ( mysqli_result $result )

只需要一个参数。

这里是 http://php.net/manual/ru/mysqli-result.num-rows.php 文档

【讨论】:

    【解决方案3】:

    mysqli_num_rows 甚至与mysql_result 没有任何相似之处。

    在这种情况下,mysqli 中mysql_result 的替换是获取整行并仅使用第一个元素,例如;

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

    【讨论】:

      【解决方案4】:

      正确的做法是

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

      【讨论】:

        猜你喜欢
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        相关资源
        最近更新 更多