哪个适合我的目的mysqli_stmt_get_result或mysqli_stmt_bind_result?

时间:2017-09-29 23:17:53

标签: php mysqli

Php Folks,

你会注意到,我对我的评论有疑问,我对如何继续前进表示困惑。我问下面哪一个我应该使用哪个适合我的代码的上下文。 我评论了我个人认为不适合我的代码背景的那个。但是,我需要你的专业意见。

//$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ?

$result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // Which line to use ? This line or the one above ?

这是一个登录页面脚本,用户可以选择通过键入“用户名”或“电子邮件”登录其帐户。

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

include 'config.php';

// check if user is already logged in
if (is_logged() === true) 
{
    //Redirect user to homepage page after 5 seconds.
    header("refresh:2;url=home.php");
    exit; //
}


if ($_SERVER['REQUEST_METHOD'] == "POST")
{ 
    if (isset($_POST["login_username_or_email"]) && 
isset($_POST["login_password"]))
    {
        $username_or_email = trim($_POST["login_username_or_email"]); //
        $password = $_POST["login_password"];       

        //Select Username or Email to check against Mysql DB if they are 
        already registered or not.
        $stmt = mysqli_stmt_init($conn);

        if(strpos("$username_or_email", "@"))
        {
            $email = $username_or_email;
            $username = "";

            $query = "SELECT ids, usernames, passwords, emails, 
            accounts_activations_statuses FROM users WHERE emails = ?";
            $stmt = mysqli_prepare($conn, $query);          
            mysqli_stmt_bind_param($stmt, 's', $email);
            mysqli_stmt_execute($stmt);
            //$result = mysqli_stmt_get_result($stmt); //Which line to use ? 
            This line or the next ?
            $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, 
            $db_password, $db_email, $db_account_activation_status); // 
            Which line to use ? This line or the one above ?
        }
        else
        {
            $username = $username_or_email;
            $email = "";
            $query = "SELECT ids, usernames, passwords, emails, 
            accounts_activations_statuses FROM users WHERE usernames = ?";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, 's', $username);
            mysqli_stmt_execute($stmt);
            //$result = mysqli_stmt_get_result($stmt); //Which line to use ? 
            This line or the next ?
            $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, 
            $db_password, $db_email, $db_account_activation_status); // 
            Which line to use ? This line or the one above ?
        }           

        $row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 
        2 of the next 2 ?           

        mysqli_stmt_close($stmt);

        printf("%s (%s)\n",$row["usernames"],$row["passwords"]);

        if ($result == false)
        {
            echo "No result!";// For debugging purpose!
            exit();
        }
        elseif ($row['accounts_activations_statuses'] == '0')
        {
            {
                echo "You have not activated your account yet! Check your 
                email for instructions on how to activate it. 
                Check your spam folder if you don't find an email from us.";
                exit();
             }
        }
        else
        {

            if (password_verify($password, $db_password))       
            {
                echo "IF triggered for password_verify! password_verify ok"; 
                // For debugging purpose!

                $_SESSION["user"] = $username;
                header("location:home.php?user=$username");             
        }
        else
        {
            echo "Incorrect User Credentials !';<br>";
            exit();
        }
    }
}

?>

<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="">
    <h3><?= $site_name ?> Member Login Form</h3>
    <fieldset>
        <label for="login_name">Username/Email:</label>
        <input type="text" name="login_username_or_email" id="login_name" 
        value="">
        <br>
        <label for="login_pass">Password:</label>
        <input type="password" name="login_password" id="login_pass" 
        value="">
    </fieldset>
    <div class="submitsAndHiddens">
        <label for="login_remember">Remember Login Details:</label>
        <input type="checkbox" name="login_remember" />
        <br>
        <button type="submit">Login</button>
        <br>
        <a href="login_password_reset.php">Forgot your Password ? Reset it 
        here!</a>
        <br>
        <a href="register.php">Register here!</a>
    </div>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

mysqli_stmt_get_result()仅适用于MYSQLND驱动程序。如果您没有安装此功能,则必须使用mysqli_stmt_bind_result()来获取准备好的声明的结果。

如果您安装了驱动程序,则可以使用mysqli_stmt_get_result(),然后您可以将结果作为关联数组获取,这通常比mysqli_stmt_bind_result更方便。