您好我有一个简单的数据库连接测试php文件(index.php)应该用include语句调用另一个页面(output.html.php),但它似乎不起作用。请帮忙。以下是两个文件的代码。谢谢。
index.php
:
<?php
try
{
$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
$pdo->setAttribute(PDO::ALTER_ERRMODE, PDO::ERRMODE_EXCEPTION);
$PDO->exec('SET NAMES "utf8");
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include = 'output.html.php';
exit();
}
$output = 'Database connection established.';
include 'output.html.php';
?>
output.html.php
:
<!DOCTYPE html>
<html lang="en-us">
<meta charset="UTF-8" >
<title>DB Example</title>
<head>
</head>
<body>
<p>
<?php echo $output; ?>
</p>
</body
</html>
我只是得到空白页。
答案 0 :(得分:2)
include 'output.html.php';
不
include = 'output.html.php';
删除=
答案 1 :(得分:1)
index.php:
<?php
try
{
$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
$pdo->setAttribute(PDO::ALTER_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("SET CHARACTER SET utf8");//$PDO->exec('SET NAMES "utf8");
header("location:output.html.php?msg=Database%20connection%20established");
}
catch (PDOException $e)
{
header("location:output.html.php?msg=Unable%20to%20connect%20to%20the%20database%20server");
}
?>
我理解如果你包含文件它重定向到那个空白的页面,因此没有按钮($输出)显示。如果你应用重定向方法,你移动到失败和成功所需的消息显示。
output.html.php: -
<!DOCTYPE html>
<html lang="en-us">
<meta charset="UTF-8" >
<title>DB Example</title>
<head>
</head>
<body>
<p>
<?php if(isset($_GET['msg'])){echo $_GET['msg'];} ?>
</p>
</body
</html>
答案 2 :(得分:0)
可能你在问题中错过了字符串的结尾并且已经解决了它。无论如何,这里有一点错误:index.php,首先尝试{}阻止:
...
$PDO->exec('SET NAMES "utf8");
...
应该是:
$PDO->exec('SET NAMES "utf8"');
希望它对你有所帮助。正如您在答案和评论中看到的那样,在PHP开发环境中启用错误是解决此问题的最佳方法&#34;白屏死机&#34;错误。以下是关于最后一个主题的参考:http://www.php.net/manual/en/errorfunc.configuration.php#89648