我正在尝试使用一个表单,我希望当用户提交此表单时 mySQL 进入我的数据库并获取与登录用户匹配的帐户 ID。我在获取用户名时没有问题,但我没有'不知道如何使用用户名从 mySQL 获取帐户 ID。
所以我要问的是如何告诉 mySQL 去获取与登录的用户名匹配的帐户 ID,然后将帐户 ID 保存在变量中。
假设这是表的结构:
user: id|username|otherInfo
select id from user where username='your submitted username';
从结果集中提取并做任何事情
这里详细说明,假设是表的结构:
user: account_id|username|otherInfo
//get your posted value(s) and maybe sanitize a bit
$username=htmlentities($_POST['username']);
/* assuming you set up your db connection, using PDO for this example, i'll
* call that variable $db
*/
//use prepared statement
$pstmt=$db->prepare("select * from user where username=:username");
$pstmt->execute(array(':username'=>$username));
while($row=$pstmt->fetch(PDO::FETCH_ASSOC)){
$id=$row['account_id']; //extracted id
}