MYSQLI查询获取单个结果

时间:2013-10-09 03:49:28

标签: php mysql mysqli get

我只需从mysql数据库中获取用户名为X的成员的id。 这只能用一个while循环完成,还是有其他方法吗?

我在想的是:

$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)

谢谢,

2 个答案:

答案 0 :(得分:7)

您可以使用:

mysqli_fetch_array();

// For Instance

$id_get = mysqli_query($con, "SELECT id FROM membrs WHERE username='$username' LIMIT 1");

$id = mysqli_fetch_array($id_get);

echo $id['id']; // This will echo the ID of that user

// What I use is the following for my site:

$user_get = mysqli_query($con, "SELECT * FROM members WHERE username='$username'");

$user = mysqli_fetch_array($user);

echo $user['username']; // This will echo the Username

echo $user['fname']; // This will echo their first name (I used this for a dashboard)

答案 1 :(得分:2)

没有while循环我们可以通过以下代码来完成,如果你选择了多于1条记录,你需要循环它

$row = mysqli_fetch_array($id);
echo $row['id'];