获取错误:尝试获取非对象的属性

时间:2018-04-28 14:20:14

标签: php

我在Trying to get property of non-object inif ($fetch_size_id->num_rows > 0)处获得此else if ($fetch_size_id->num_rows == 0)

无法弄清楚导致它的原因。 if条件也不起作用......

当我var_dump($fetch_size_id)时,我得到bool(false)

        $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'])."'
            AND `sizes`.`ratio` = '".$conn->real_escape_string($row['6'])."'
            AND `sizes`.`construction` = '".$conn->real_escape_string($construction)."'
            AND `sizes`.`diameter` = '".$conn->real_escape_string($row['7'])."
        ");

        if ($fetch_size_id->num_rows > 0) { 
            $fetch_size_id = $fetch_size_id->fetch_object(); 
            $temp_size_id = $fetch_size_id->size_id;

        } else if ($fetch_size_id->num_rows == 0) { 
            //create new

        } else { 
            //error 
        }

我忽略了什么吗?

1 个答案:

答案 0 :(得分:0)

您没有检查变量$fetch_size_id

  if (isset ($fetch_size_id){
    if ($fetch_size_id->num_rows) { 
        $fetch_size_id = $fetch_size_id->fetch_object(); 
        $temp_size_id = $fetch_size_id->size_id;
      } else{ 
        //create new
      }
    } else { 
        //error 
    }