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

求助:PHP报错Trying to get property of non-object问题排查

Fixing "Trying to get property of non-object" with mysqli Query Results

Hey there, let's break down exactly what's happening here and get this sorted out quickly.

The Core Problem

When you ran var_dump($fetch_size_id) and got bool(false), that tells you your SQL query failed to execute—it didn't even return an empty result set. The mysqli query() method only returns a result set object if the query runs successfully (even if no rows match). When it fails, it returns false, which is why trying to access $fetch_size_id->num_rows throws that "non-object" error—you're trying to call an object property on a boolean value.

Step 1: Find Out Why the Query Failed

First, let's get the actual error message from MySQL. Add this right after your query() call to see what's going wrong:

$fetch_size_id = $conn->query("SELECT `sizes`.`size_id`, `sizes`.`width`, `sizes`.`ratio`, `sizes`.`construction`, `sizes`.`diameter` FROM `sizes` WHERE `sizes`.`width` = '" . $conn->real_escape_string($row['5']) . "'");

// Add this error check to reveal the issue
if (!$fetch_size_id) {
    die("Query failed: " . $conn->error);
}

This will print the exact problem—common culprits include:

  • Typos in table/column names (e.g., size instead of sizes)
  • Unclosed clauses or syntax errors in your SQL (your original query has ellipses, so double-check if you finished the WHERE condition properly)
  • Database permission issues (your DB user doesn't have access to the sizes table)

Step 2: Verify Your SQL Directly

Another quick debug trick is to print the full SQL string before executing it, then run it directly in your database tool (like phpMyAdmin or Navicat):

$sql = "SELECT `sizes`.`size_id`, `sizes`.`width`, `sizes`.`ratio`, `sizes`.`construction`, `sizes`.`diameter` FROM `sizes` WHERE `sizes`.`width` = '" . $conn->real_escape_string($row['5']) . "'";
echo "Debug SQL: " . $sql; // Copy this output and run it in your DB tool
$fetch_size_id = $conn->query($sql);

If the SQL throws an error in your DB tool, that's the exact problem you need to fix.

Step 3: Fix Your Condition Logic

Once your query returns a valid result set object, you can safely check num_rows. Also, you don't need else if ($fetch_size_id->num_rows == 0)—a simple else works because if the query succeeded, num_rows is either greater than 0 or exactly 0:

$fetch_size_id = $conn->query($sql);

if ($fetch_size_id) {
    if ($fetch_size_id->num_rows > 0) {
        // Handle rows that were found
        while ($row = $fetch_size_id->fetch_assoc()) {
            // Process each row's data here
        }
    } else {
        // No matching rows found
        echo "No sizes match the specified width.";
    }
} else {
    // Query failed—handle the error gracefully
    echo "Database error: " . $conn->error;
}

Bonus: Use Prepared Statements (Safer & Less Error-Prone)

While real_escape_string helps, prepared statements are more secure against SQL injection and avoid syntax errors from manual string concatenation. Here's how to rewrite your query with them:

// Prepare the statement (use ? as a placeholder for the value)
$stmt = $conn->prepare("SELECT size_id, width, ratio, construction, diameter FROM sizes WHERE width = ?");

// Bind the value to the placeholder: "s" means string (use "i" for integers)
$stmt->bind_param("s", $row['5']);

// Execute the query
$stmt->execute();

// Get the result set
$result = $stmt->get_result();

// Check and process rows
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Process your data here
    }
} else {
    echo "No matching sizes found.";
}

// Clean up the statement
$stmt->close();

内容的提问来源于stack exchange,提问作者Nkk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:23:24