2013-10-09 57 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'];

相關問題