从mySQL表中获取信息

时间:2012-10-23 20:35:05

标签: php mysql get

我正在尝试使用表单,我希望当用户提交此表单以便mySQL进入我的数据库并获得与登录用户匹配的帐户ID时。我对获取用户名没有问题但我不知道如何使用用户名从mySQL获取帐户ID。

所以我要问的是我如何告诉mySQL去获取与登录用户名匹配的帐户ID,然后将帐户ID保存在变量中。

2 个答案:

答案 0 :(得分:1)

假设这是表格的结构:

user:  id|username|otherInfo

select id from user where username='your submitted username';

从结果集中提取并执行任何操作

答案 1 :(得分:0)

此处有详细说明,假设这是表的结构:

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        
}
相关问题