PHP脚本在新主机环境下无法运行,请求技术排查
Hey there, let's work through troubleshooting your GetGPS.php script that's failing on the new host. Based on the code snippet you shared, here are the most common issues to check and fix:
1. Validate Database Connection Setup
First off, the new host's database settings are almost certainly different from your old one.
- Double-check the credentials in
init.php:$server_name,$mysql_user,$mysql_pass, and$db_nameneed to match the new host's database details. Many hosts use a dedicated database hostname (not justlocalhost) or restrict access to specific IPs. - Add connection error debugging right after creating the mysqli object to get clear failure details:
$conn = new mysqli($server_name, $mysql_user, $mysql_pass, $db_name); // Add this line to debug connection issues if ($conn->connect_error) { die("DB Connection Failed: " . $conn->connect_error); }
2. Confirm POST Data is Being Received
Your script relies on $_POST variables, so let's make sure the data is actually reaching it:
- Temporarily add this at the top of the POST block to see what's coming through:
if($_SERVER['REQUEST_METHOD'] == 'POST') { var_dump($_POST); // Print all received POST data exit; // Stop execution to check the output // Rest of your code... }
If the output is empty, either:
- The Android app is sending data in the wrong format (your script expects
application/x-www-form-urlencodedormultipart/form-data, not raw JSON) - The new host's PHP config has
post_max_sizeorupload_max_filesizeset too low (since you're handling image data, this is a common culprit)
3. Fix the Incomplete SQL Query
Your code snippet cuts off at $GetOldIdSQL ="SELECT id FROM gpsmessag... — this incomplete query will throw a syntax error every time.
- Finish the query (e.g.,
SELECT id FROM gpsmessage WHERE userid = ?assuming your table is namedgpsmessage) - Critical: Never directly insert
$_POSTvalues into SQL strings — this opens you up to SQL injection. Use prepared statements instead:
// Replace your incomplete query with this secure version $stmt = $conn->prepare("SELECT id FROM gpsmessage WHERE userid = ?"); $stmt->bind_param("s", $userid); // "s" denotes a string parameter type $stmt->execute(); $result = $stmt->get_result();
4. Check PHP Version & Extensions
- New hosts often run newer PHP versions (like 8.x) which might have breaking changes from older versions (e.g., PHP 5.x). Confirm your mysqli syntax is compatible with the new version.
- Verify the
mysqliextension is enabled on the new host: Create a temporaryphpinfo.phpfile with<?php phpinfo(); ?>, access it in your browser, and search for "mysqli" to ensure it's loaded.
5. Check File Permissions & Enable Error Logging
- Ensure
GetGPS.phpandinit.phphave correct file permissions (usually644for files,755for directories) — incorrect permissions can prevent the script from running or reading configs. - Turn on full error reporting to catch hidden issues: Add this at the very top of
GetGPS.php:
error_reporting(E_ALL); ini_set('display_errors', 1);
This will show you any PHP errors that might be silently failing right now.
内容的提问来源于stack exchange,提问作者Mors-
相关产品推荐
相关产品推荐

