PHP显示空白页面

时间:2013-06-18 21:17:37

标签: php

请帮助我是php新手。和学习php我想为我的组织创建一个项目。我已经安装了wordpress。现在我想从Mysql数据库创建一个网页,以表格格式显示日期这是我的代码。哪里错了我找不到?它显示一个空白页面。这是为了测试purpouse

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tables from MySQL Database</title>

<style type="text/css">
table.db-table          { border-right:1px solid #ccc; border-bottom:1px solid #ccc; }
table.db-table th       { background:#eee; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
table.db-table td       { padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
</style>

</head>
<body>
<table>
<?php
/* connect to the db */
$connection = mysql_connect('localhost','user','password');
mysql_select_db('test',$connection);

if (!@mysql_select_db('test',$connection)) {
exit('<p>Unable to locate the test ' . 
'database at this time.</p>');
}

$result = mysql_query("SELECT Annual_Sales, Name,address,phone FROM exampleco");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>   
<tr>
    <td><?=$row['Annual_Sales']; ?></td>
    <td><?=$row['name']; ?></td>
    <td><?=$row['address']; ?></td>
    <td><?=$row['phone']; ?></td>
</tr>  
<?php
}

mysql_free_result($result);

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

2 个答案:

答案 0 :(得分:2)

试试这个......它删除了mysql_select_db()函数的错误抑制,并在Web浏览器中强制显示错误。

第一次修订

<?php
error_reporting(E_ALL); // Report on all errors
ini_set('display_errors', '1'); // Display those errors through the web page.
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tables from MySQL Database</title>

<style type="text/css">
table.db-table          { border-right:1px solid #ccc; border-bottom:1px solid #ccc; }
table.db-table th       { background:#eee; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
table.db-table td       { padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
</style>

</head>
<body>
<table>
<?php
/* connect to the db */
$connection = mysql_connect('localhost','user','password');
mysql_select_db('test',$connection);

if (!mysql_select_db('test',$connection)) {
exit('<p>Unable to locate the test ' . 
'database at this time.</p>');
}

$result = mysql_query("SELECT Annual_Sales, Name,address,phone FROM exampleco");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>   
<tr>
    <td><?=$row['Annual_Sales']; ?></td>
    <td><?=$row['name']; ?></td>
    <td><?=$row['address']; ?></td>
    <td><?=$row['phone']; ?></td>
</tr>  
<?php
}

mysql_free_result($result);

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

每当您使用PHP进行编程并且代码尚未准备好进行生产时,我高度会鼓励您确保显示错误。 PHP错误通常非常有用,可以大大帮助开发过程!

修订版2

包含更多打印功能,以帮助追踪问题。

<?php
print 'Started.<br />';

error_reporting(E_ALL); // Report on all errors
ini_set('display_errors', '1'); // Display those errors through the web page.

print 'Errors Now Showing. To test, the following line should display an error:<br />';
print $a_var_which_doesnt_exist;
print '<br />';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tables from MySQL Database</title>

<style type="text/css">
table.db-table          { border-right:1px solid #ccc; border-bottom:1px solid #ccc; }
table.db-table th       { background:#eee; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
table.db-table td       { padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
</style>

</head>
<body>
<table>
<?php
/* connect to the db */
$connection = mysql_connect('localhost', 'user', 'password');

if(!$connection) { // Ensure connection successful.
    die('<p>Could not connect: ' . mysql_error() . '</p>');
}

$db_sel = mysql_select_db('test', $connection);

if($db_sel) { // Ensure we're able to select database.
    die('<p>Could not select DB: ' . mysql_error() . '</p>');
}

$result = mysql_query('SELECT Annual_Sales, Name,address,phone FROM exampleco') or die('<p>Error with the query: ' . mysql_error() . '</p>');

if(mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    ?>   
    <tr>
        <td><?=$row['Annual_Sales']; ?></td>
        <td><?=$row['name']; ?></td>
        <td><?=$row['address']; ?></td>
        <td><?=$row['phone']; ?></td>
    </tr>  
    <?php
    }
} else {
    ?>
    <tr><td colspan="4">NO ROWS RETURNED BY QUERY</td></tr>
    <?php
}

mysql_free_result($result);

?>
</table>
</body>
</html>
<?php print 'Finished.<br />';

答案 1 :(得分:0)

您正在使用某个函数调用之前的@符号。这将使该函数抛出的任何错误无声。

点击此处了解详情:http://php.net/manual/en/language.operators.errorcontrol.php

尝试删除@前面的@mysql_select_db('test',$connection),看看是否有任何内容。