pdo没有约束力

时间:2012-10-10 18:41:31

标签: php pdo mysqli

请尝试从mysqli移动到pdo但运行此代码

          <?php
  $sql = "select concat(register.fname ,' ' ,register.lname) as name,register.matric   as matric,register.username as uname,register.sex as sex,register.phone as phone ,register.passport as passport ,register.email as email,register.level as level,faculty.fac_name as fac,dept.dept_name as dept,diary.diary as diary,diary.date_added as added from register,faculty,dept,diary where register.user_id = :_id  and diary.username = :name limit 1";
$sth = $dbh->prepare($sql);
$sth->bindValue(':id', $id, PDO::PARAM_INT);
$sth->bindParam(':name', $username);
$sth->execute();


    ?> 

它显示此错误(!)警告:PDOStatement :: bindValue()[pdostatement.bindvalue]:SQLSTATE [HY093]:参数号无效:参数未在C:\ wamp \ www \ uni \ det \ viewuser中定义第14行的.php

3 个答案:

答案 0 :(得分:1)

在查询中,您已将其命名为绑定值:_id中的:id

答案 1 :(得分:0)

您有:_id,请在bindValue电话

中进行更改

答案 2 :(得分:0)

您将变量$id绑定到:id,但在声明中需要:_id

$sth->bindValue(':_id', $id, PDO::PARAM_INT);

或者更改声明:

  $sql = "select concat(register.fname ,' ' ,register.lname) as name,register.matric   as matric,register.username as uname,register.sex as sex,register.phone as phone ,register.passport as passport ,register.email as email,register.level as level,faculty.fac_name as fac,dept.dept_name as dept,diary.diary as diary,diary.date_added as added".
           "from register,faculty,dept,diary ".
          " where register.user_id = :id  and diary.username = :name limit 1";