PHP脚本本地XAMPP运行正常,服务器仅显示空白页求助
Hey Rahul, blank pages on production are almost always due to suppressed errors—let's break this down step by step to fix it:
1. First, Uncover Hidden Errors
Production servers usually disable error display for security, but we can temporarily enable it to see exactly what's breaking. Add this at the very top of your script:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
This will replace the blank screen with a specific error message (like a SQL syntax issue or undefined variable) that points us to the problem.
2. Fix Critical SQL Logic Bug
Looking at your first query, there's an operator priority mistake that's almost certainly causing unexpected results (or no results) on production:
Your current WHERE clause:
WHERE sale_status='paid' OR sale_status='in process' AND YEAR(date) = YEAR(CURDATE())
Since AND has higher priority than OR, this gets interpreted as:sale_status='paid' OR (sale_status='in process' AND YEAR(date) = CURRENT YEAR)
This is almost definitely not what you intended. Wrap the OR conditions in parentheses to group them correctly:
WHERE (sale_status='paid' OR sale_status='in process') AND YEAR(date) = YEAR(CURDATE())
This ensures both statuses are filtered to only entries from the current year.
3. Fix the Truncated SQL Query
Your second query is cut off mid-statement:
$bar2=$conn->query("SELECT DATE_FORMAT(date,'%m')as mn,sum(amou...");
If this is how your actual code exists, it's a syntax error (unclosed string, incomplete sum() function). Finish the query—for example, if the column name is amount, update it to:
$bar2=$conn->query("SELECT DATE_FORMAT(date,'%m')as mn,sum(amount)as am FROM ENTRY WHERE [your conditions here] GROUP BY mn");
4. Add Error Checks for Database Operations
You're assuming the database connection and queries always succeed—add safeguards to catch failures:
- Verify the connection works:
require_once("config.php"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
- Check if queries execute successfully:
$bar = $conn->query("YOUR CORRECTED FIRST QUERY HERE"); if (!$bar) { die("First query failed: " . $conn->error); }
Repeat this check for $bar2 to catch any SQL syntax or production database permission issues.
5. Minor PHP Version Edge Case
While PHP 7.1 vs 7.2 shouldn't break your core logic, initialize the $arry array before using it to avoid implicit initialization quirks:
$arry = []; // Add this right before your while loop while($arr=$bar->fetch_assoc()){ $arry[array_shift($arr)]=$arr['am']; }
Once you implement these steps, you'll either see the specific error to fix or your script will start working as expected.
内容的提问来源于stack exchange,提问作者Rahul

