如何使用 PHP MySQLi 获取行数并使用 mysql_fetch_array() 用于使用过程方法准备的语句?

How to use PHP MySQLi to get number of rows and use mysql_fetch_array() for prepared statments using the procedural method?

我正在尝试重新学习 PHP、SQL、HTML、CSS 和 JS,因为我上次在 Uni 学习它已经 2 年了。

我有以下 mysqli 语句,它们要么不能正常工作,要么会产生奇怪的结果:

更新 下面是我的 connectdb.php 文件中的代码:

   <?php

    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "testdatabase";

    $dbc = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    ?>

请假设所有未定义的变量都已定义,我保证问题与语法无关,而是与 mysqli 模块有关,在切换到使用准备语句方法后变得有点困难。

<?php
$sub_signin_email = $_POST["signin_email"];

require("connectdb.php");
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$q = "SELECT * FROM `users` WHERE `email`= ? ";

$stmt = mysqli_prepare($dbc, $q);

if ($stmt){

    $bind = mysqli_stmt_bind_param($stmt, 's', $sub_signin_email);
    if (!$bind){ echo "Error! Unable to bind stmt <br/>"; }

    $exec = mysqli_stmt_execute($stmt);
    if (!$exec){ echo "Error! Unable to execute stmt <br/>"; }

    $result = mysqli_stmt_get_result($stmt);
    $stored_result = mysqli_stmt_store_result($stmt);

    $num_of_rows = mysqli_stmt_num_rows($stmt);
?>
    <table>
        <thead>
            <th>User Id</th>
            <th>Title</th>
            <th>FName</th>
            <th>LName</th>
            <th>Email</th>
            <th>Password</th>
            <th>Account_Type</th>
            <th>Status</th>
        </thead>

        <tbody> 
<?php
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
        $userId = $row['user_id'];
        $title = $row['Title'];
        $Fname = $row['FName'];
        $Lname = $row['LName'];
        $email = $row['email'];
        $password = $row['password'];
        $account_type = $row['account_type'];
        $status = $row['status'];

        echo "<tr>";
        echo "<td> $userId </td>";
        echo "<td> $title </td>";
        echo "<td> $Fname </td>";
        echo "<td> $Lname </td>";
        echo "<td> $email </td>";
        echo "<td> $password </td>";
        echo "<td> $account_type </td>";
        echo "<td> $status </td>";
        echo "<td>";
        echo "</tr>";
     }
?>
        </tbody>
    </table>
<?php
        }
        mysqli_stmt_close($stmt);     
?>

经过数小时的在线研究,我得出了上述最终解决方案,但是尽管它有效,mysqli_stmt_num_rows($stmt);,无论有几条记录或没有记录,似乎总是 return 0。

为了克服这个问题,我发现我只需要使用 mysqli_stmt_store_result($stmt);,一旦使用它有助于 mysqli_stmt_num_rows($stmt); 正常工作,但是当它到达处理点时会导致错误 mysqli_fetch_array($result, MYSQLI_ASSOC).对于这种情况,我收到一个错误,例如:mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given**

或者,当我只使用 mysqli_stmt_get_result($stmt); 时,它会执行所有操作,但它还会将行数显示为 0。

请不要让我开始使用 PDO,我目前只想使用我所知道的,即 MySQLi,我尝试使用 PDO,但是因为我不擅长 OOP ,我觉得很难用。

非常感谢你:D.

我相信 fetch assoc 会更适合你。您正在使用列名,并使用 while() 循环,因此您无需以数字方式导航结果数据(除了知道有多少)...

$result = mysqli_stmt_get_result($stmt);
$stored_result = mysqli_stmt_store_result($stmt);
$num_of_rows = mysqli_stmt_num_rows($stmt);
if($num_of_rows > 0){
 while($row = mysql_fetch_assoc($result)){
  // declare variables (ex: $user = $row['user'])
 }
} else {
 echo "<tr><td colspan='8'>No results found...</td></tr>"; 
}

让我知道这是否适合您...没有明显的原因表明您的尝试失败了,但是无法看到您的数据库连接变量 var 转储或类似的错误测试方法,它将是很难帮你。

你能尝试回显一些 sql 错误吗?

如果没有任何效果,如果直到最后都不需要行数,请执行此操作...

$numResults = 0; 
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
 // Declare variables 
 $numResults++; 
}
echo $numResults; // number of rows found, after loop 

哇,今天找了将近5个多小时的解决方案,终于搞定了,惭愧,解决方案这么简单

因为我不熟悉使用 mysqli 创建准备好的语句,所以我认为我只能使用与其相关的过程(即在函数名称中包含 stmt 的过程),因此 1 这样的函数我太专注于尝试获得工作是:

$num_of_rows = mysqli_stmt_num_rows($stmt);

使用mysqli_stmt_store_result()方法:

到目前为止,我被迫使用 mysqli_stmt_store_result($stmt);,以便 mysqli_stmt_num_rows 上面 return 我从查询中 return 得到正确的行数。但是,使用此 mysqli_stmt_store_result() 过程导致我在尝试执行 while($row = mysqli_fetch_assoc($result))

时遇到错误

使用mysqli_stmt_get_result()方法:

因为我无法获取数据,我在网上搜索并找到了替代程序,即mysqli_stmt_get_result($stmt);,现在这个功能使我能够毫无问题地获取mysqli_fetch_assoc($result)),这让我能够从查询中访问 returned 值并随意操作它们。然而,当使用这种方法时 mysqli_stmt_num_rows($stmt); 总是 returned 0 而不管我的查询从数据库中找到的匹配项的数量。

经过数小时的搜索找到了解决方案

为了找到查询找到的行数,使用:

$result = mysqli_stmt_get_result($stmt);

是,而不是使用:

$num_of_rows = mysqli_stmt_num_rows($stmt);

我只需要使用:

$num_of_rows = mysqli_num_rows($result);

现在终于可以了。它 return 是查询中 return 的总行数,还允许我使用 while($row = mysqli_fetch_assoc($result)) 获取和操作查询中 return 的行项目。

此外,通过使用这种方法,我不再需要使用:

mysqli_stmt_store_result($stmt);

使用 mysqli 从我的数据库中获取数据。

抱歉回答太长,我已经在网上搜索了几个小时的解决方案,但没有找到合适的解决方案,因此我希望这可以帮助其他面临类似问题的人。

感谢所有在这里和聊天中帮助我尝试找到解决方案的人。