PHP查询有结果但返回空JSON数组,求排查解决方法
groupe_académiques Query Let's break down the possible issues and fix this step by step—since your script works for other tables, the problem is likely specific to this table or how you're interacting with it.
1. Fix Character Set Mismatch (Critical for Accented Characters)
Your table name includes a French accented character (é in académiques), which is a common hidden culprit. If your database connection's character set doesn't match the table's collation, MySQL might fail to recognize the table name correctly—resulting in an empty result set without throwing an obvious error.
Quick Fix: Add this line right after your database connection in conn.php to enforce full Unicode support:
// Object-oriented mysqli syntax $conn->set_charset('utf8mb4'); // Or procedural syntax, if that's what you use in conn.php // mysqli_set_charset($conn, 'utf8mb4');
2. Enable Error Reporting to Catch Hidden Issues
You mentioned no error messages are showing up—PHP or MySQL might be suppressing them. Let's force error display to uncover what's going wrong:
Add these lines at the very top of your script:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require "conn.php"; // ... rest of your code
Then add explicit error checking right after running your query:
$query = "SELECT * FROM groupe_académiques"; $result = $conn->query($query); // Catch query errors immediately if (!$result) { die("Query failed: " . $conn->error); }
This will show you if there's a table name typo, permission issue, or other query-related problem you're missing.
3. Verify the Result Set Before JSON Encoding
Sometimes json_encode() returns an empty string if your array contains non-UTF-8 characters. Let's check what's actually in your $response array before encoding:
Add a var_dump() right before the json_encode line:
// ... your while loop here } var_dump($response); // Check if this shows your actual data echo json_encode($response);
- If
var_dump()shows an empty array: Double-check yourgroupe_académiquestable in phpMyAdmin/MySQL CLI to confirm it actually has data. - If
var_dump()shows data butjson_encode()is empty: YourNomfield has non-UTF-8 characters. Fix this by ensuring your table usesutf8mb4collation and your connection is set toutf8mb4(as in step 1).
4. Stick to One mysqli API Style (Avoid Edge Cases)
You're mixing object-oriented ($conn->query()) and procedural (mysqli_num_rows(), mysqli_fetch_array()) mysqli methods. While this usually works, switching to a consistent style can eliminate rare edge cases:
// Use object-oriented syntax throughout $number_of_rows = $result->num_rows; while ($row = $result->fetch_array()) { array_push($response, array("ID"=>$row[0], "Nom"=>$row[1])); }
5. Double-Check Table Name Case Sensitivity
On Linux-based MySQL servers, table names are case-sensitive. Make sure the name in your query exactly matches the actual table name in the database (including accents and capitalization—groupe_académiques vs Groupe_Académiques are treated as different tables).
内容的提问来源于stack exchange,提问作者Salim Hallouche

