douyo770657 2011-09-09 07:26
浏览 53
已采纳

pdo错误:产品子变体:产品:(致命错误:在非对象上调用成员函数prepare())

I am using pdo in php and the error i am getting is:

Product sub variant: Product: (
Fatal error: Call to a member function prepare() on a non-object

i have this piece of code in a class and i call it in php, the class is included in the page im calling it.

function get_variantname($variant_id)
    {
        $query = $db->prepare("select parent_attribute_label from tbl_parent_attribute where parent_attribute_id = :variant_id");
        $query->bindParam(":variant_id", $variant_id);
        $query->execute();

        while($row = $query->fetch(PDO::FETCH_OBJ))
        {
            return $row->parent_attribute_label;            
        }
    }
  • 写回答

2条回答 默认 最新

  • douzhizao0270 2011-09-09 07:34
    关注

    You are trying to incorrectly access the $db variable, which looks like a global. Add

    global $db;
    

    at the beginning of your function.

    The error message tells you that prepare is being called on something that is not an object. But we all know it is an object, since you set it once to some value (there's no chance that it might be e.g. a string). That leaves only the possibility of it not having the value you set inside the function, for some reason. Which leads us to the answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?