准备好的语句返回错误号0

时间:2019-05-26 23:11:57

标签: php mysql prepared-statement

问题:我正在设置我的登录系统,该系统应该允许用户使用用户名或密码登录。但是,我的prepare语句将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语句似乎不起作用:/

1 个答案:

答案 0 :(得分:1)

您正在两次调用prepare()。

您在不带参数的情况下调用prepare()时在if条件中执行了一个空语句...因此没有任何事情可以做,也没有错误(错误号0),但是该调用的结果为false。只需检查第一次准备调用的结果即可。

更改第二个预备呼叫:

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>"; }
相关问题