18

我只是想弄清楚如何确定行数,然后在 HTML 中显示该数字。

我准备好的声明如下所示:

if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ?ORDER BY id ASC")) 
    {
    /* Bind parameters, s - string, b - blob, i - int, etc */
    $stmt -> bind_param("i", $id);
    $stmt -> execute();
    
    /* Bind results */
    $stmt -> bind_result($testfield1, $testfield2, $testfield3);
    
    /* Fetch the value */
    $stmt -> fetch();

    /* Close statement */
    $stmt -> close();
   }

我知道我应该先保存结果,然后使用num_rows,如下所示:

$stmt->store_result();
$stmt->num_rows;

但是,我正在运行,并且当我将代码放在那里时页面出现问题。我什至无法进入如何显示行数的下一步

在计算准备好的语句中的行数方面我缺少什么,那么我将如何用<?php echo '# rows: '.$WHATGOESHERE;?>

4

4 回答 4

22

num_rows returns the number, you have to store it in a variable.

/*.....other code...*/
$numberofrows = $stmt->num_rows;
/*.....other code...*/
    
echo '# rows: '.$numberofrows;

So full code should be something like this:

$stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ? ORDER BY id ASC");
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("i", $id);
$stmt -> execute();
$stmt -> store_result();

/* Bind results */
$stmt -> bind_result($testfield1, $testfield2, $testfield3);

/* Fetch the value */
$stmt -> fetch();
$numberofrows = $stmt->num_rows;

/* Close statement */
$stmt -> close();

echo '# rows: '.$numberofrows;
于 2013-05-24T00:31:52.450 回答
3

如果您只对行数而不是实际的数据行感兴趣,这里有一个完整的查询块,COUNT(*)在 SELECT 子句中调用。

$conn = new mysqli("host", "user", "pass", "db");
$stmt = $conn->prepare("SELECT COUNT(*) FROM `table` WHERE id= ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->bind_result($num_rows);
$stmt->fetch();
echo $num_rows;

或者,如果您想在迭代/处理行之前知道行数,一种方法是将整个结果集(多维数组)集中到一个变量中并count()在迭代之前调用。

$conn = new mysqli("host", "user", "pass", "db");
$sql = "SELECT field1, field2, field3
        FROM table
        WHERE id= ?
        ORDER BY id";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
$resultset = $result->fetch_all(MYSQLI_ASSOC);
echo "<div>Num: " , count($resultset) , "</div>";
foreach ($resultset as $row) {
    echo "<div>Row: {$row['field1']} & {$row['field2']} & {$row['field3']}</div>";
}

*我已经在我的本地主机上测试了以上两个片段都可以成功。

于 2018-07-10T07:47:29.553 回答
1

这适用于2020 年 2 月

$number_of_records = $stmt->rowCount();
echo $number_of_records;

来自 php.net 手册:

PDOStatement::rowCount() 返回受 DELETE、INSERT 或 UPDATE 语句影响的行数。

这是他们网站上的一个例子

<?php
/* Delete all rows from the FRUIT table */
$del = $dbh->prepare('DELETE FROM fruit');
$del->execute();

/* Return number of rows that were deleted */
print("Return number of rows that were deleted:\n");
$count = $del->rowCount();
print("Deleted $count rows.\n");
?>

上面的示例将输出:

Return number of rows that were deleted:
Deleted 9 rows.
于 2020-02-26T14:03:40.587 回答
-3

Check out the example #2 here: PHP.net

Use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

于 2013-05-24T00:31:17.597 回答