php pdo未绑定值

vfh0ocws  于 2023-10-15  发布在  PHP
关注(0)|答案(3)|浏览(105)

我试图从mysqli迁移到pdo,但遇到了一个问题。

$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();

显示此错误:
警告:PDO语句::bindValue()[pdostatement.bindvalue]:SQLSTATE[HY093]:无效的参数号:会话信息:SESSION_ID = mjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj

jum4pzuy

jum4pzuy1#

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

vjhs03f7

vjhs03f72#

您有:_id,请在bindValue调用中更改它

tuwxkamq

tuwxkamq3#

你将变量$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";

相关问题