从数据库中选择无效参数?

时间:2014-04-29 16:06:20

标签: php mysql

它的问题很小,但我一直在看这个年龄,但仍然无法让它工作,我得到两个错误

警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:

中未定义参数

警告:PDOStatement :: execute():SQLSTATE [HY093]:

中的参数号无效

我的代码是:

 public function getdata ($tran_id)
     {
        $sql = "SELECT tran_id, seller_user_name, user_name_buyer 
                FROM trade_transaction, feedback Where  feedback.feedback_username = trade.user_name_of_buyer 
              AND user_name_of_buyer = :user_name_buyer ";
         $sth = $this->db->prepare($sql);
         $sth->execute(array(':tran_id' => $tran_id, ':user_name_buyer ' => $_SESSION['user_name']));

         $user = $sth->fetch();

3 个答案:

答案 0 :(得分:3)

您在致电:tran_id期间绑定了execute参数,但您未在查询中使用该参数。

将您的execute行更改为此

$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));

答案 1 :(得分:0)

从参数列表中删除:tran_id或为该参数添加条件。我希望这有帮助。

答案 2 :(得分:0)

你的select语句没有tran_id的where子句,要么从执行调用中删除tran_id

$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));

或在sql语句中添加一个额外的where子句

$sql = "SELECT tran_id, seller_user_name, user_name_buyer 
                FROM trade_transaction, feedback
          WHERE  feedback.feedback_username = trade.user_name_of_buyer
              AND tran_id = :tran_id 
              AND user_name_of_buyer = :user_name_buyer ";