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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号