PHP 准备好的语句问题

2021-12-25 00:00:00 php mysqli prepared-statement

慢慢习惯了 PHP 准备好的语句,但仍然出现这个错误

slowly getting used to PHP prepared statements, however still get this error

警告:mysqli_stmt::bind_result():绑定变量的数量与第 20 行 C:UsersPCDocumentsXAMPPhtdocslogin.php 中准备好的语句中的字段数不匹配".

"Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:UsersPCDocumentsXAMPPhtdocslogin.php on line 20".

 <?php

 $mysqli = new mysqli('localhost', 'c3337015', 'c3337015', 'members');

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

 if(isset($_GET['loginEmail'])){

 session_start();

    $stmt = $mysqli->prepare("SELECT Email FROM members WHERE Email=? AND  Password=? LIMIT 1");
    $email = $_GET['loginEmail'];
    $password = $_GET['loginPassword'];
    $password = sha1($password);
    $stmt->bind_param('ss', $email, $password);
    $stmt->execute();
    $stmt->bind_result($email, $password);
    $stmt->store_result();
    if($stmt->num_rows == 1)  //To check if the row exists
        {
            while($stmt->fetch()) //fetching the contents of the row

              {$_SESSION['Logged'] = 1;
               $_SESSION['Email'] = $email;
               header('Location: index.php');
               exit();
               }
        }
        else {
            echo "Wrong Username or Password!";
        }
        $stmt->close();
    }
    else 
    {   
    echo "Something went Wrong";
    }
$mysqli->close();
?>

推荐答案

$stmt->bind_result($email, $password);

您正在绑定 2 个变量,但只要求一个:SELECT Email FROM members.

You are binding 2 variables, but only asking for one: SELECT Email FROM members.

我还建议对 bind_result 使用不同的变量,因为它和 bind_param 都适用于引用.

I'd also suggest using different variables for bind_result, as it and bind_param both work on references.

$stmt->bind_result($userEmail);

相关文章