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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同