准备好的语句 return 错误数 0

Prepared statement return errors number 0

问题:我正在设置我的登录系统,应该允许用户使用用户名或密码登录。但是我的准备语句 returns Prepare failed: (0) 到控制台(0 是 mysql 错误号)。

我尝试过的方法:我尝试查看 MySQL 手册以查看 Error Number 0 是什么,但找不到任何内容。

<?php
include "../includes/config.php";
if($_SERVER["REQUEST_METHOD"] == "POST") {
        // Can be either the email or username 
    $login = $_POST['login'];
    $password = $_POST['password'];

    if ( $login != "" && $password != "" ) {
        // Prepare the statement
        $pullLogin = $link->prepare("SELECT hashed_password FROM users WHERE (username = '?' OR email='?')");
        // Check Prepare
        if ( !$pullLogin->prepare() ) { echo "<script>console.log('Prepare failed: ($pullLogin->errno) $pullLogin->error');</script>"; }
        // Bind the parameters to the statement
        $pullLogin->bind_param( "ss", $login, $login );
        // Execute it :-)
        $pullLogin->execute();
        // Store the results, I don't really knwo why but whatever
        $pullLogin->store_result();
        // Bind the password to a variable :-)
        $pullLogin->bind_result($correctPassword);
        // Free the results
        $pullLogin->free_results();
        // Close the statement
        $pullLogin->close();



        // This is hashed and I'm only echoing it to see if ^ works
        echo $correctPassword;


    }
}
?>

预期:用户可以使用他们的用户名或电子邮件登录。
实际结果:准备语句似乎不起作用:/

你重复调用了 prepare()。

当调用不带参数的 prepare() 时,您正在 if-condition 中执行一个空语句...所以没有什么可做的,也没有错误(错误号 0),但是该调用的结果是假的。只需检查第一个 prepare-call.

的结果

改第二个prepare-call:

if ( $login != "" && $password != "" ) {
  // Prepare the statement
  $pullLogin = $link->prepare("SELECT hashed_password FROM users WHERE (username = '?' OR email='?')");
  // Check Prepare
  if ( !$pullLogin->prepare() ) { echo "<script>console.log('Prepare failed: ($pullLogin->errno) $pullLogin->error');</script>"; }

以下内容,所以您只测试第一个 prepare() 的结果:

if ( $login != "" && $password != "" ) {
  // Prepare the statement
  $pullLogin = $link->prepare("SELECT hashed_password FROM users WHERE (username = '?' OR email='?')");
  // Check Prepare
  if ( !$pullLogin ) { echo "<script>console.log('Prepare failed: ($link->errno) $link->error');</script>"; }