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条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line