You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP网站HTTP ERROR 500报错求助:未定义类常量ERR_WARNING

Fixing the HTTP 500 Error & Undefined PDO Constant Issue

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:56:47