2012-07-13 85 views
1

我創建了一個網站,它有多個登錄信息和唯一信息..我想從一個用戶檢索數據。例如我的用戶名是qwert,我的密碼是1234,我想檢索他的唯一信息到數據庫。我使用w3schools中的示例代碼,它選擇所有的數據,但我想要做的只是從用戶那裏只檢索數據。在mysql中使用php檢索單行數據

任何人都可以幫助我嗎?任何幫助都感激不盡。

mysql_select_db("xone_login", $con); 

$result = mysql_query("SELECT * FROM admin WHERE username = '$myusername' "); 

echo "<table border='1'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['overtime'] . "</td>"; 
    echo "<td>" . $row['daily_rate'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

mysql_close($con); 
?> 
+0

你可以在這裏添加你的代碼,以便我們可以看到需要做什麼? – andrewsi 2012-07-13 19:48:41

+0

您需要一個'WHERE'子句。 'SELECT * FROM yourtable WHERE username ='the_logged_in_username'' – 2012-07-13 19:49:50

+0

歡迎來到StackOverflow。沒有任何代碼示例,我們無法知道你想要做什麼。 – Lion 2012-07-13 19:50:52

回答

2

與本該教程替換SQL代碼(和調整表和列名)之一:

SELECT * FROM USERS where name ='qwert' and pass = MD5('1234') 

,照顧在消毒的變量,以避免SQL注入攻擊!

+1

我認爲,還應該提及一些關於SQL注入的內容。 – Lion 2012-07-13 19:56:43

-1
You need to use a where clause 
Also you will need to specify limits on the query to restrict the result set to 1 record 

$select = "SELECT * FROM usertable WHERE username = '$user' LIMIT 0, 1"; 
$query = mysql_query($select) or die(mysql_error()); 
$result = mysql_fetch_assoc($query); 

//Prints the array 
print_r($result); 
+1

這有零SQL轉義,並作爲一個不行的例子。 – tadman 2012-07-13 20:22:05