PHP网站HTTP ERROR 500报错求助:未定义类常量ERR_WARNING
Hey there, let's troubleshoot and fix your issue right away! The core problem here is a typo in a PDO constant name, plus a small logic oversight in your function. Let's break it down step by step:
1. The Undefined Constant Error (Root Cause)
In your connect.php file, you're using PDO::ERR_WARNING on line 3—but this constant doesn't exist in PDO! The correct constant for enabling warning-style error reporting is PDO::ERRMODE_WARNING. That's exactly why you're seeing the "undefined class constant" error triggering the HTTP 500.
2. Fix connect.php
Replace your current connect.php code with this corrected version:
<?php $bdd = new PDO('mysql:host=localhost;dbname=blog;charset=utf8','root','root'); $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); ?>
3. Bonus Fix: Unreachable Code in functions.php
Looking at your getArticles() function, the line $req->closeCursor(); comes after return $data;—this means it will never run, since return exits the function immediately. Move it before the return statement to properly clean up the database cursor:
<?php // ARTICLE FUNCTION function getArticles() { require "./config/connect.php"; $req = $bdd->prepare('SELECT id, title FROM articles ORDER BY id DESC'); $req->execute(); $data = $req->fetchAll(PDO::FETCH_OBJ); $req->closeCursor(); // Moved this line before return to execute it return $data; } ?>
4. Verify the Fix
After making these two changes:
- Restart your MAMP server to ensure the changes take effect
- Refresh your test page—this should resolve the HTTP 500 error and load your articles properly
内容的提问来源于stack exchange,提问作者Hatsingue

