我为我正在进行的项目设计了一个授权系统。我能够从数据库中检索用户ID,但是,我现在正在尝试为用户获取用户名和电子邮件。 顺便说一下,我对PHP很新。
我收到了这个错误:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
我很困惑,因为
$stmt->execute()
工作正常,但是
$stmt2->execute()
失败了。我确定问题很简单,但我无法弄明白。这是用于获取信息的PHP代码。
$stmt = $dbh->prepare("SELECT nuewavedb_user_id, nuewavedb_username, nuewavedb_password FROM nuewavedb_users
WHERE nuewavedb_username = :nuewavedb_username AND nuewavedb_password = :nuewavedb_password");
$stmt2 = $dbh->prepare("SELECT nuewavedb_fullname, nuewavedb_email FROM nuewavedb_users
WHERE nuewavedb_username = :nuewavedb_username AND nuewavedb_password = :nuewavedb_password");
/*** bind the parameters ***/
$stmt->bindParam(':nuewavedb_username', $nuewavedb_username, PDO::PARAM_STR);
$stmt->bindParam(':nuewavedb_password', $nuewavedb_password, PDO::PARAM_STR, 40);
$stmt2->bindParam(':nuewavedb_fullname', $nuewavedb_fullname, PDO::PARAM_STR);
$stmt2->bindParam(':nuewavedb_email', $nuewavedb_email, PDO::PARAM_STR);
/*** execute the prepared statement ***/
$stmt->execute();
$stmt2->execute();
/*** check for a result ***/
$user_id = $stmt->fetchColumn();
$nuewavedb_fullname = $stmt2->fetchColumn();
$nuewavedb_email = $stmt2->fetchColumn(1);
提前感谢您的帮助!
答案 0 :(得分:1)
你应该以某种方式组合这个查询,特别是如果它是同一个表,不需要读两次
$stmt = $dbh->prepare("SELECT
nuewavedb_user_id
,nuewavedb_username
,nuewavedb_password
,nuewavedb_fullname
,nuewavedb_email
FROM nuewavedb_users
WHERE nuewavedb_username = :nuewavedb_username AND
nuewavedb_password = :nuewavedb_password"
);
然后执行并正确获取:
/*** bind the parameters ***/
$stmt->bindParam(':nuewavedb_username', $nuewavedb_username, PDO::PARAM_STR);
$stmt->bindParam(':nuewavedb_password', $nuewavedb_password, PDO::PARAM_STR, 40);
/*** execute the prepared statement ***/
$stmt->execute();
/*** fetch **/
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$nuewavedb_fullname = $row['nuewavedb_fullname'];
$nuewavedb_email = $row['nuewavedb_email'];
答案 1 :(得分:0)
在语句stmt2中定义2个参数:: nuewavedb_username和:nuewavedb_password 但是当你绑定你使用的参数时:nuewavedb_fullname和:nuewavedb_email
您必须使用相同的参数名称