1

Hi I have a simple database connection test php file (index.php) that is supposed to call another page (output.html.php) with the include statement but it does not seem to work. Please help. Below is the code for both files. Thank you.

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>

I just get blank page.

4

3 回答 3

2
include 'output.html.php'; 

不是

include = 'output.html.php';

去除=

于 2013-06-28T23:46:16.343 回答
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>
于 2013-06-29T08:10:45.443 回答
0

可能您在问题中遗漏了字符串的结尾并且已经解决了它。无论如何,这里有一个小错误:index.php, first try{} 块:

...
$PDO->exec('SET NAMES "utf8");
... 

它应该是:

$PDO->exec('SET NAMES "utf8"');

希望对您有所帮助。正如您在答案和评论中看到的那样,在 PHP 开发环境中启用错误是解决此“死机白屏”错误的最佳方法。这是关于最后一个主题的参考:http ://www.php.net/manual/en/errorfunc.configuration.php#89648

于 2013-06-29T07:09:29.517 回答