从XAMPP迁移至树莓派Apache服务器遇PHP属性获取报错求助
Hey there! I’ve run into this exact issue before when moving local XAMPP projects to Raspberry Pi, so let’s walk through the most common fixes. That notice means your code is trying to access a property (using ->) on something that isn’t an object—usually a false value or an array instead. Here’s how to track it down:
1. Verify Your Database Connection First
Chances are, the problem starts with connecting to MySQL on the Pi, which has different default settings than XAMPP:
- XAMPP often uses a passwordless
rootuser, but Raspberry Pi’s MySQL (typicallymariadb-server) sets a password during installation. Double-check your connection credentials in your PHP code. - Add error checking to your connection code to confirm it’s working:
$conn = new mysqli("localhost", "your_username", "your_password", "your_db"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } - Sometimes, the Pi’s MySQL binds to
127.0.0.1instead of using the Unix socket thatlocalhostrefers to. Try replacinglocalhostwith127.0.0.1in your connection string if you’re getting timeouts.
2. Fix Query Result Handling
Even if the connection works, your query might be returning false (if it failed) or no rows, and you’re trying to treat it like an object:
- Always validate query results before accessing properties. For example, if fetching a single row:
$sql = "SELECT * FROM users WHERE id = 1"; $result = $conn->query($sql); // First check if the query succeeded if (!$result) { die("Query failed: " . $conn->error); } // Then check if any rows were returned if ($result->num_rows > 0) { $user = $result->fetch_object(); // Now safely access $user->username echo $user->username; } else { echo "No matching record found"; } - If you’re using
mysqli_fetch_array()instead offetch_object(), you’ll get an array, not an object—so use$user['username']instead of$user->username. This is a common mix-up between environments.
3. Check Database & PHP Environment Differences
- Data & Schema: Make sure you’ve fully imported your XAMPP database into the Pi’s MySQL. Even a missing table or row can cause queries to return empty results. Use
mysqldumpfrom XAMPP and import it on the Pi to keep everything identical. - PHP Extensions: Confirm that the
mysqliextension is enabled on the Pi. Runphp -m | grep mysqliin the terminal—if it doesn’t show up, install it withsudo apt install php-mysqliand restart Apache (sudo systemctl restart apache2). - PHP Version: XAMPP might run a newer PHP version than the Pi’s default. Check for deprecated functions or syntax differences (e.g., older PHP versions handle object returns differently). You can check versions with
php -von both systems.
Quick Debugging Tip
Add var_dump() right before the line throwing the error to see what value you’re actually working with:
// Line before the error: var_dump($your_variable); // Then the line that throws the notice: echo $your_variable->property;
This will tell you if $your_variable is false, null, or an array—immediately pointing you to the root cause.
If you can share the specific code snippet where the error occurs, we can narrow this down even further!
内容的提问来源于stack exchange,提问作者user9604534

