【问题标题】:mysqli_num_rows() expects parameter 1 to be mysqli_result [duplicate]mysqli_num_rows() 期望参数 1 为 mysqli_result [重复]
【发布时间】:2014-12-10 19:16:36
【问题描述】:

您好,我正在尝试查看 Selected 查询的内容...但是出现错误:

mysqli_num_rows() 期望参数 1 为 mysqli_result ...

      $stmt= $this->conn->prepare("SELECT * FROM table") or die(mysql_error());
        $result = $stmt->execute();


        // check for empty result
        if (mysqli_num_rows($result) > 0) {

    ..
    }

编辑 新错误 mysqli_fetch_array() 期望参数 1 为 mysqli_result,对象在...中给出...

    $stmt= $this->conn->prepare("SELECT * FROM table") or die(mysqli_error());

$stmt->execute();

        $stmt->store_result();

// check for empty result
if ($stmt->num_rows > 0) {
    // looping through all results

    $response["array"] = array();

    while ($row = mysqli_fetch_array($stmt)) { 

... }

我想我明白了:

    while ($row =  $stmt->fetchAll()) {
        // temp user array
        $array= array();
        $array["bla"] = $row["bla"];
... }

但我只得到“null”作为值...?

编辑

现在我明白了:

$stmt->bind_result($column1, $column2...)

然后

$array["bla"] = $column1; $array["bla2"] = $column2;

但是是否可以绑定所有列而不将每个列放入变量中? 或者是不是可以使用这个:

$array["bla"] = $row["bla"];

所以它将“bla”列的行中的值放入$array[bla]?

因为我解决它的方式似乎很困难

【问题讨论】:

  • 您将 OOP 与标准代码混合在一起。这是行不通的。解决这个问题,你应该很高兴
  • 还将 MySQL API 与 mysql_error() 混合使用

标签: php mysql mysqli


【解决方案1】:

正确的是

   $stmt= $this->conn->prepare("SELECT * FROM table") or die(mysql_error());
        $result = $stmt->execute();
        $result->store_result();

        // check for empty result
        if ($result->num_rows > 0) {

    ..
    }

【讨论】:

  • 你能解释一下 $result = $stmt->execute(); $result->store_result();做?第一个结果是来自 SELECT 查询的内容,对吗?但是第二行是什么意思呢?
  • 您可以执行以下操作:$stmt = $this->conn->query("SELECT * FROM table"); if($stmt->num_rows >= 1) { return true; } else { return false; } 或者,如果您想使用准备好的语句(此查询不是必需的),请执行以下操作:$stmt = $this->conn->prepare("SELECT * FROM table"); // No need to bind_params $stmt->execute(); $stmt->store_result(); if($stmt->num_rows >= 1) { return true; } else { return false; }
  • 从数据库连接上的最后一个查询传输结果集 => php.net/manual/ro/mysqli.store-result.php
  • @SuperDJ 好的 - 如何处理 mysqli_fetch_array($stmt){ } 这也不起作用:S 错误:mysqli_fetch_array() 期望参数 1 是 mysqli_result,对象在...中给出跨度>
  • @Steve 这取决于您如何调用查询。使用标准 mysqli_query()prepared 或 OOP query
猜你喜欢
  • 1970-01-01
  • 2018-11-06
  • 2017-05-15
  • 2013-07-17
  • 1970-01-01
  • 2011-01-05
  • 2013-06-02
  • 1970-01-01
  • 2013-03-04
相关资源
最近更新 更多