2014-05-14 58 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

相关问题