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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?