代码之家  ›  专栏  ›  技术社区  ›  SShah Marc B

如何使用PHP-MySQLi获取行数,并使用mysql\u fetch\u array()使用过程方法来准备语句?

  •  0
  • SShah Marc B  · 技术社区  · 8 年前

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

    我有以下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模块有关,在切换到使用prepared statement方法后,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); ,似乎总是返回0,无论是否有多条记录。

    为了克服这个问题,我发现我只需要 mysqli_stmt_store_result($stmt); ,它曾经帮助 mysqli\u stmt\u num\u行($stmt); 正常工作,但到达处理点时会导致错误 mysqli_fetch_array($result, MYSQLI_ASSOC) 。在这种情况下,我收到一个错误,例如: mysqli_fetch_array() 参数1应为 mysqli_result ,给定布尔值**

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

    请不要要求我开始使用PDO,我目前只想使用我知道的东西,即MySQLi,我尝试使用PDO,但是由于我不擅长OOP,我发现很难使用。

    非常感谢:D。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Nerdi.org    8 年前

    我相信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>"; 
    }
    

    如果对你有用,请告诉我。。。没有明显的原因表明您的尝试失败了,但是如果无法看到db连接变量var dump或类似的错误测试方法,那么很难帮助您。

    您可以尝试回显一些sql错误吗?

    如果什么都不起作用,如果在结束前不需要#行,请执行此操作。。。

    $numResults = 0; 
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
     // Declare variables 
     $numResults++; 
    }
    echo $numResults; // number of rows found, after loop 
    
        2
  •  2
  •   SShah Marc B    8 年前

    哇,今天花了将近5个多小时寻找解决方案,最后我终于明白了,我感到很惭愧,因为解决方案太简单了。

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

    $num_of_rows = mysqli_stmt_num_rows($stmt);
    

    使用 mysqli_stmt_store_result() 方法:

    到目前为止,我被迫使用 mysqli_stmt_store_result($stmt); ,以便 mysqli_stmt_num_rows 以返回查询返回的正确行数。但是,使用此 mysqli\u stmt\u store\u result() 程序导致我在尝试执行时遇到错误 while($row = mysqli_fetch_assoc($result))

    使用 mysqli_stmt_get_result() 方法:

    因为我无法获取数据,我在网上搜索并找到了另一个过程 mysqli_stmt_get_result($stmt); ,现在此函数使我能够获取 mysqli_fetch_assoc($result)) 没有问题,这允许我访问查询返回的值,并根据自己的意愿对其进行操作。但是,使用此方法时 mysqli_stmt_num_rows($stmt); 无论我的查询从数据库中找到多少匹配项,始终返回0。

    搜索数小时后找到的解决方案

    要查找查询找到的行数,请使用:

    $result = mysqli_stmt_get_result($stmt);
    

    而不是使用:

    $num\u of\u rows=mysqli\u stmt\u num\u rows($stmt);
    

    我所要做的就是:

    $num_of_rows = mysqli_num_rows($result);
    

    现在终于可以了。它返回从查询返回的总行数,还允许我使用 while($行=mysqli\u fetch\u assoc($结果)) 获取和操作从查询返回的行项目。

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

    mysqli_stmt_store_result($stmt);
    

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

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

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