Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 哪一个适合我的目的mysqli\u stmt\u get\u result还是mysqli\u stmt\u bind\u result?_Php_Mysqli - Fatal编程技术网

Php 哪一个适合我的目的mysqli\u stmt\u get\u result还是mysqli\u stmt\u bind\u result?

Php 哪一个适合我的目的mysqli\u stmt\u get\u result还是mysqli\u stmt\u bind\u result?,php,mysqli,Php,Mysqli,各位 你会注意到,我对我的评论有一个问题,我对如何继续下去感到困惑。我问我应该使用下面2个选项中的哪一个,它将很好地适合我的代码上下文。 我把我个人认为不适合我的代码上下文的那个注释掉了。但是,我需要你的专业意见 //$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ? $result = mysqli_stmt_bind_result($stmt, $db_id, $db_

各位

你会注意到,我对我的评论有一个问题,我对如何继续下去感到困惑。我问我应该使用下面2个选项中的哪一个,它将很好地适合我的代码上下文。 我把我个人认为不适合我的代码上下文的那个注释掉了。但是,我需要你的专业意见

//$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 ?
这是一个登录页面脚本,用户可以选择通过键入“用户名”或“电子邮件”登录其帐户


mysqli\u stmt\u get\u result()
仅适用于
MYSQLND
驱动程序。如果未安装此命令,则必须使用
mysqli\u stmt\u bind\u result()
获取准备语句的结果


如果您安装了驱动程序,您可以使用
mysqli\u stmt\u get\u result()
,然后您可以将结果作为关联数组来获取,这通常比
mysqli\u stmt\u bind\u result

更方便。看起来您的问题已经得到了回答,但还有一点需要注意。您可以将条件简化为
$column=strpos($username\u或电子邮件“,“@”)!=假的?”电子邮件':'用户名'然后只使用1个查询、1个绑定、1个执行。只需绑定
$username\u或\u email
,并在查询中使用
$column
。。假设我读对了,没有其他区别。(您也从未将
$username
设置为第二个值,如果是这样,
$\u会话
不会设置该值)您向我显示的那行代码是pdo oop样式正确的吗?我还没有说到那一点。所以,如果你不介意的话,我可以看看mysqli的程序化编码方式吗?谢谢
PDO
mysqli
是不同的驱动程序。过程式和面向对象编程都是编码风格,三元运算符并不是其中一种特定的。各位,你们介意在我的原始帖子中回顾我的代码,并在你们认为有必要编辑的地方进行编辑吗?我更喜欢两个版本,一个使用mysqli_stmt_get_结果,另一个使用mysqli_stmt_bind_结果。这样,我们新手就可以通过你的例子向你学习。没人知道答案?好的。我想我的网络主机已经安装了。那么,您认为我到目前为止在mysqli_stmt_bind_result()上的编码方式还可以吗?或者,您认为我应该忘记它,而是取消mysqli_stmt_get_result()的注释吗?如果您已经以一种方式编写了它,那么没有理由更改它。但将来可以使用更简单的样式,就像在不使用预先准备好的语句时使用
mysqli\u fetch\u assoc()
一样。
<?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>