duan010167787 2017-08-14 13:58
浏览 170

致命错误:未捕获错误:调用未定义的方法stdClass :: count()

I got this error in products.php page

Fatal error: Uncaught Error: Call to undefined method stdClass::count() in C:\xampp\htdocs\shopCart avigation.php:29 Stack trace: #0 C:\xampp\htdocs\shopCart\layout_head.php(27): include() #1 C:\xampp\htdocs\shopCart\products.php(15): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\shopCart avigation.php on line 29

This is the error part of navigation.php page. Last line is 29

// count products in cart
$cart_item = new \stdClass();
$cart_item->user_id=1; // default to user with ID "1" for now
$cart_count=$cart_item->count();

This is count function in cartItem class

class CartItem{

    // database connection and table name
    private $conn;
    private $table_name = "cart_items";

    // object properties
    public $id;
    public $product_id;
    public $quantity;
    public $user_id;
    public $created;
    public $modified;

    // constructor
    public function __construct($db){
        $this->conn = $db;
    }
    // count user's items in the cart
    public function count() {

            // query to count existing cart item
            $query = "SELECT count(*) FROM " . $this->table_name . " WHERE user_id=:user_id";

            // prepare query statement
            $stmt = $this->conn->prepare( $query );

            // sanitize
            $this->user_id=htmlspecialchars(strip_tags($this->user_id));

            // bind category id variable
            $stmt->bindParam(":user_id", $this->user_id);

            // execute query
            $stmt->execute();

            // get row value
            $rows = $stmt->fetch(PDO::FETCH_NUM);

            // return
            return $rows[0];
        }
}

how can i solve it?

  • 写回答

1条回答 默认 最新

  • dousuitang5239 2017-08-14 14:01
    关注

    You are instantiating $cart_item as a stdClass object. I am assuming from your code example and class it should be instantiated as a CartItem object, like so:

    $cart_item = new CartItem($db);
    

    You were getting the error because stdClass doesn't have a count() method.

    评论

报告相同问题?

悬赏问题

  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答