2013-10-09 38 views
1

我只需要从mysql数据库中得到用户名是X的成员的id。 这只能用while循环完成,还是有其他解决方法?MYSQLI查询得到一个单一的结果

我在想什么的是一样的东西:

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

感谢,

+0

可能的重复:http://stackoverflow.com/questions/11456707/single-value-mysqli – Ashish

回答

6

您可以使用:

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) 
+0

为什么要投票?只是出于好奇 –

+0

我没有downvote,但可能是因为你使用'$ id = mysqli_fetch_array($ id);'它可能应该是'$ show_id = mysqli_fetch_array($ id);'then'echo $ show_id [ 'id'];' - 我说“可能”。好像你只会重置'$ id' –

+0

我通常不会这样做。我会更新答案。 –

2

不while循环,我们可以通过下面的代码做到这一点,如果你是选择超过1个记录您需要循环它

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

为了得到它在同一行(无需对$id_get变量):

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

然后显示它,只是做echo $id['id'];

相关问题