SQLSTATE [HY093]:参数号无效:未定义参数#

时间:2018-06-18 07:49:06

标签: php mysql

我正在编写一些代码,通过以下代码与phpmyadmin上的登录数据库进行交互。

但是,我收到以下错误,它没有指定错误发生在哪一行。它已经困扰了我3天,我的研究没有导致任何解决方案。以防万一:SQLSTATE [HY093]:参数号无效:参数未定义

有人可以帮我解决问题吗?

提前谢谢。

<?php
session_start();

$user_name = "test" ;
$user_password = "test";

/* Login to database */
$DB_name = "adaming_login_db";
$DB_user = "root";
$DB_pass = "";
$DB_host = "localhost";

try
{
     $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
     $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
     echo $e->getMessage();

}

class USER
{
    private $db;

    public function __construct($DB_con)
    {
      $this->db = $DB_con;
    }

    public function login($user_name,$user_password)
    {
       try
       {

          $stmt = $this->db->prepare("SELECT * FROM login_info WHERE user_name = :$user_name AND user_password = :$user_password");
          $stmt->execute(array(':user_name'=>$user_name, ':user_password'=>$user_password));
          $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
          if($stmt->rowCount() > 0)
          {
             if($user_password == $userRow['user_password'] && $user_name == $userRow['user_name'] )
             {
                $_SESSION['user_session'] = $userRow['user_id'];
                echo "Success";
                return true;
             }
             else
             {
                 echo "Failed";
                return false;
             }
          }
       }
       catch(PDOException $e)
       {
           echo $e->getMessage();
       }
   }


}
$user = new USER($DB_con);
$user->login($user_name,$user_password); 


?> 

1 个答案:

答案 0 :(得分:0)

您需要从查询中删除美元符号。变化:

$stmt = $this->db->prepare("SELECT * FROM login_info WHERE user_name = :$user_name AND user_password = :$user_password");

$stmt = $this->db->prepare("SELECT * FROM login_info WHERE user_name = :user_name AND user_password = :user_password");