![](/img/trans.png)
[英]Fatal error: Uncaught Error: Call to undefined method stdClass::option();
[英]Fatal error: Uncaught Error: Call to undefined method stdClass::count()
我在products.php頁面中遇到此錯誤
致命錯誤:未捕獲錯誤:調用C:\\ xampp \\ htdocs \\ shopCart \\ navigation.php:29中未定義的方法stdClass :: count()堆棧跟蹤:#0 C:\\ xampp \\ htdocs \\ shopCart \\ layout_head.php(27 ):include()#1 C:\\ xampp \\ htdocs \\ shopCart \\ products.php(15):include('C:\\ xampp \\ htdocs ...')#2 {main}拋出C:\\ xampp \\ htdocs第29行的\\ shopCart \\ navigation.php
這是navigation.php頁面的錯誤部分。 最后一行是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();
這是cartItem類中的count函數
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];
}
}
我該如何解決?
您將$cart_item
實例$cart_item
stdClass
對象。 我假設從您的代碼示例和類中,應將其實例化為CartItem
對象,如下所示:
$cart_item = new CartItem($db);
由於stdClass
沒有count()
方法,因此出現錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.