PHP 准备好的语句问题

时间:2023-03-07
本文介绍了PHP 准备好的语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

慢慢习惯了 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);

这篇关于PHP 准备好的语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:mysqli_fetch_array 只返回一个结果 下一篇:调用未定义的函数 mysqli_result::num_rows()

相关文章