2010-06-17 143 views
0

有誰知道如何檢索一段數據並在php文件中顯示結果? ,我會進入一個類似的查詢是這樣的:PHP - 從MySQL服務器檢索數據

SELECT 'email' FROM 'users' WHERE 'username' = 'bob' 

因此,其結果必然是剛剛的電子郵件。

感謝,

凱文

回答

4

要連接

$con = mysql_connect("host","userName","passWord"); 
if (!$con){die('Could not connect: ' . mysql_error());} 
mysql_select_db("dbNameToSelect", $con); 

要查詢

$result = mysql_query("SELECT `email` FROM `users` WHERE `username` = 'bob'"); 
while($row = mysql_fetch_array($result)){ 
    // do stuff with $row['colName'] 
} 

要關閉連接mysql_close($con);

+0

我將如何連接到數據庫? – lab12 2010-06-17 19:19:44

1

您需要連接並從數據庫中獲取數據,如mysql。檢查下面的代碼:

$server = "localhost"; 
$username = "root"; 
$password = ""; 
$database = "mysampledb"; 

mysql_connect($server, $username, $password); 
mysql_select_db($database); 

$result = mysql_query("SELECT email FROM users WHERE username = 'bob'") or die(mysql_error()); 

while($row = mysql_fetch_assoc($result)) { 
    echo $row["email"]; 
}