SQLSTATE [HY093]:参数号无效[string:Exception:private]

时间:2015-11-30 17:04:54

标签: php mysql

  

PDOException对象([message:protected] => SQLSTATE [HY093]:参数号无效[string:Exception:private] => [code:protected] => HY093 [file:protected] => / home / unfed / public_html / src / php / auth / login.php [line:protected] => 74 [trace:Exception:private] => Array([0] => Array([file] => /home/unfed/public_html/src/php/auth/login.php [line] => 74 [function] =>执行[class] => PDOStatement [type] => - > [args] = >数组([0] =>数组([:用户名] =>测试)))[1] =>数组([file] => / home / unfed / public_html / src / php / auth / login.php [line] => 137 [function] => query [args] =>数组([0] => INSERT INTO用户(用户名,密码,电子邮件,图片)VALUES(:用户名,:密码) ,:email,'test')[1] =>数组([:用户名] =>测试)[2] =>数组([:密码] =>测试)[3] =>数组( [:email] =>这里有一封电子邮件)))[2] =>数组([file] => /home/unfed/public_html/signup/index.php [line] => 53 [function] => register [args] =>数组([0] =>测试[1] =>这里有一封电子邮件[2] =>测试)))[上一篇:例外:私人] => [errorInfo] =>数组([0] => HY093))

不知道为什么会发生这种情况。 继承人我的PHP:

//Create a new function named LoggedIn, And apply database info;
function Connect($host = 'localhost',$username = 'unfed_admin',$password = 'x',$dbname = 'unfed_auth') {

    //Try execute the PHP with no errors;
    try {

        //Create a PDO Session;
        $con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

        //Session Attributes;
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    }

    //Catch all PDOException errors;
    catch (PDOException $e) {

        //If any errors print result;
        echo "<code><pre>".print_r($e)."</pre></code>";

        //Make the PDO session false;
        $con = false;
    }

    //If no errors happened Make the PDO session true;
    return $con;
}
//Create a new function named query;
function query($sql = false,$bind = false,$obj = false) {

    //Prepare The SQL Query;
    $query = Connect()->prepare($sql);

    $res = true;

    //Execute Binded Query;
    try { $query->execute($bind); $res = true; }

    catch (PDOException $e) { 

        //If any errors print result;
        echo "<code><pre>".print_r($e)."</pre></code>";

        $res = false;

    }

    //If no errors happened Make $row true;
    return $res;

}
//Create a new function named user_login;
function register($username = false, $email = false, $password = false) {

    global $registeroutput;

    //Fetch for the username and email to see if they are already used;
    $fetchusername = fetch("SELECT username FROM users WHERE username = :username",array(":username"=>$username));
    $fetchemail = fetch("SELECT email FROM users WHERE email = :email",array(":email"=>$email));
    if(empty($fetchusername)) {

        if(empty($fetchemail)) {

            //Register the user;
            query("INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,'test')",array(":username"=>$username),array(":password"=>$password),array(":email"=>$email));
            $registeroutput = 'Success';

        } else { $registeroutput = 'Email Error'; }

    } else { $registeroutput = 'Username Error'; }

    //If no errors happened Make the $valid true;
    return true;

}

忽略PHP代码上的注释标记没有更新它们是相关的。我知道为什么会发生这种情况。

1 个答案:

答案 0 :(得分:1)

你在这里错过了一些数组,当你的假设准备好时,导致参数不匹配:

query("INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,'test')",array(":username"=>$username),array(":password"=>$password),array(":email"=>$email));

为了解决这个问题,您可以进行以下更改:

query("INSERT INTO users (username,password,email,image) VALUES (:username,:password,:email,:image)",array(":username"=>$username),array(":password"=>$password),array(":email"=>$email), array(":image"=>'test'));

注意:imageVALUES子句中使用,然后添加到数组中。