【问题标题】:Get information from mySQL Table从 mySQL 表中获取信息
【发布时间】:2012-10-23 20:35:04
【问题描述】:

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

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

【问题讨论】:

  • 能否将代码添加到问题中?
  • @andrewsi 我没有为此工作的任何代码,因为我不知道该怎么做。我所拥有的只是用户变量。
  • 你应该从阅读一些mysql+php教程开始并熟悉它们。
  • 你至少有你要查询的表的结构吗?
  • @CrazyWebdesigner123 是的,我希望您使用“用户名”并查询它以在具有该“用户名”的行中获取“帐户 ID”

标签: php mysql get


【解决方案1】:

假设这是表的结构:

user:  id|username|otherInfo

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

从结果集中提取并做任何事情

【讨论】:

  • 如何获取变量中的 id?
  • 我没有使用 PDO..IK 它变得不受支持,但我只是使用常规的连接和查询。如何做到这些?
  • 然后只使用 fetch_assoc() 而不是 fetch(PDO::FETCH_ASSOC)
【解决方案2】:

这里详细说明,假设是表的结构:

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        
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2012-03-13
    • 2013-02-23
    相关资源
    最近更新 更多