php – 显示所有错误和警告

参见英文答案 > How do I get PHP errors to display?                                    26个
更新2:

我现在已从.php文件中删除以下内容:

<?php error_reporting( E_ALL ); ?>

我在php.ini中设置了display_erros,如下所示:

display_errors = On

错误报告在php.ini中设置为以下内容:

error_reporting = E_ALL | E_STRICT

重新启动Apache后,我仍然没有错误/警告.

更新1:

我在php.ini中更改了error_reporting:

error_reporting = E_ALL &
~E_DEPRECATED

error_reporting = E_ALL | E_STRICT

之后我重新启动了Apache,例如

/etc/init.d/apache2 restart

但页面仍然不会显示任何类型的错误/警告.

原始问题:

以下脚本正在生成警告,因为$err位于if语句中.为什么这个警告没有显示在Web浏览器的PHP页面上?

我必须查看Apache日志以查看警告.另外,如果我故意将“insert into”更改为“delete into”,它不会在PHP页面上显示错误.为什么错误不显示在实际的PHP页面上?

<?php
    error_reporting(E_ALL);
?>

<html>
    <head>
        <title></title>
        <link rel="icon" type="image/png" href="favicon.ico">

        <?php
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                $err = array();

                if (empty( $_POST['display_name']))
                    $err[] = "display name field is required";
                if (empty( $_POST['email']))
                    $err[] = "email field is required";
                if (empty( $_POST['password']))
                    $err[] = "password field is required";

                if (!$err) {
                    try {
                        $DBH = new PDO("mysql:host=localhost;dbname=database1", "user", "pass");
                        $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                        $STH = $DBH->prepare("delete into table1 (display_name, email, password) values ( :display_name, :email, :password )");

                        $STH->bindParam(':display_name', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':email', $_POST['email'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':password', $_POST['password'], PDO::PARAM_STR, 100);

                        $STH->execute();

                        $STH = $DBH->prepare("delete into table2 ( username, status, users_id ) values ( :username, :status, :users_id )");

                        $strStatus = 1;

                        $STH->bindParam(':username', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':status', $strStatus, PDO::PARAM_INT, 1);
                        $STH->bindParam(':users_id', $_POST['referer'], PDO::PARAM_INT, 1);

                        $STH->execute();

                        $DBH = null;
                    }
                    catch (PDOException $e) {
                        echo $e->getMessage();
                    }

                    header("Location: " . $_SERVER['PHP_SELF']);
                    exit;
                }
                else {
                    foreach ($_POST as $key => $val) {
                        $form[$key] = htmlspecialchars($val);
                    }
                }
            }
            else {
                $form['display_name'] = $form['email'] = $form['password'] = '';
            }
        ?>
    </head>

    <body>
        <?php foreach($err as $line) { ?>
        <div style="error"><?php echo $line; ?></div>
        <?php } ?>

        <h1>Register</h1>

        <form method="post">
            Referers id:<br/>
            <input type="text" name="referer" /><br/><br/>

            Name:<br/>
            <input type="text" name="display_name" value="<?php echo $form['display_name']; ?>" /><br/><br/>

            Email:<br/>
            <input type="text" name="email" value="<?php echo $form['email']; ?>" /><br/><br/>

            Password:<br/>
            <input type="text" name="password" value="<?php echo $form['password']; ?>" /><br/><br/>

            <input type="submit" value="register" />
        </form>
    </body>
</html>

解决方法:

可以在php.ini或Apache配置文件中关闭显示错误.

您可以在脚本中打开它:

error_reporting(E_ALL);
ini_set('display_errors', 1);

您应该在PHP错误日志中看到相同的消息.

上一篇:python – 如何在tkinter中使无声异常更大声?


下一篇:python – 仅在警告上打印消息