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

从XAMPP迁移至树莓派Apache服务器遇PHP属性获取报错求助

Troubleshooting "Trying to get property of non-object" on Raspberry Pi Apache

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 root user, but Raspberry Pi’s MySQL (typically mariadb-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.1 instead of using the Unix socket that localhost refers to. Try replacing localhost with 127.0.0.1 in 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 of fetch_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 mysqldump from XAMPP and import it on the Pi to keep everything identical.
  • PHP Extensions: Confirm that the mysqli extension is enabled on the Pi. Run php -m | grep mysqli in the terminal—if it doesn’t show up, install it with sudo apt install php-mysqli and 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 -v on 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:58:01