2014-05-14 49 views
0

我有一個數據庫,db_db我想要在我的瀏覽器顯示數據。我已經把這個php代碼(display.php)放在/ var/www /下,並試圖訪問localhost/display.php。雖然每次遇到「服務器錯誤」,但網站在檢索http://localhost/display.php時遇到錯誤。它可能因維護而關閉或配置不正確。服務器錯誤,試圖顯示數據從MySQL數據庫在PHP

的代碼如下:

<?php 
//make conn 
$link = mysql_connect('localhost', 'root', ''); 

//select db 
mysql_select_db('db_db') or die("Unable to select db"); 
$sql = "SELECT * FROM results WHERE jobid = 'abc'"; 
$records = mysql_query($sql); 
?> 


<html> 
<head> 
<title> Result Info </title> 
</head> 

<body> 
<table width="600" border = "1" cellpadding = "1" cellspacing= "1"> 
<tr> 
<th>id</th> 
<th>name</th> 
<tr> 

<?php 
while($result = mysql_fetch_assoc($records)){ 
    echo "<tr>"; 
    echo "<td>.$result['id'].</td>"; 
    echo "<td>.$result['name'].</td>"; 
    echo "</tr>"; 
}//end while 
?> 

</table> 
</body> 
</html> 

不知道我要去哪裏錯了。

+0

這聽起來更像是一個Apache的問題。你能檢索一個簡單的HTML頁面嗎? – Robbert

+0

是的,我有一個info.php(phpinfo())在相同的路徑,即/ var/www /,它返回了'localhost/info.php'上預期的值 –

+1

mysql_ *函數已被棄用。你應該考慮使用PDO或mysqli。我也在生成表格單元的代碼中看到一個錯誤。 'echo「​​。$ result ['id']。」;'應該是'echo「​​」。 $ result ['id']。 「」;' – Robbert

回答

1

數據庫選擇和查詢語法可能存在問題。 修改您的代碼

<?php 
//make conn 
$link = mysql_connect('localhost', 'root', ''); 

//select db 
mysql_select_db('db_db', $link) or die("Unable to select db"); 
$sql = "SELECT * FROM results WHERE jobid = 'abc'"; 
records = mysql_query($sql); 
?> 

檢查是否有效。祝你好運

+0

這與上面的代碼有什麼不同?除了分配一個變量給mysql_connect –

+0

它不只是分配變量,請檢查第六行。它在與指定鏈接標識符關聯的服務器上設置當前活動數據庫。請檢查http://www.php.net/manual/en/function.mysql-select-db.php – user3575290

相關問題