I do the withdrawal of goods by category, but an error occurs

Warning: PDOStatement :: execute (): SQLSTATE [HY093]: Invalid parameter number: not defined in (if the withdrawal of goods itself is not working, then I will be happy with criticism and advice) :) ProductController.php

class ProductModel extends Model { public function products($idcat){ $stmt=$this->db->prepare("SELECT id, name,price,description FROM products WHERE category_id = ?"); $stmt->bindParam('i',$idcat); $stmt ->execute(); $result = array(); $i=1; while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $result[$i]['id']=$row['id']; $result[$i]['name']=$row['name']; $result[$i]['description']=$row['descriprion']; $result[$i]['price']=$row['price']; $i++; } return $result; } } 

ProductController.php

 require_once (MODEL_PATH ."/CategoryModel.php"); class ProductController extends Controller { private $pageTpl = "/Views/templates/product.tpl.php"; public function __construct() { $this->model=new ProductModel(); $this->view= new View(); } public function index(){ $this->pageData['title']="О нас"; $category=CategoryModel::Get_Category(); $this->pageData['category'] = $category; $idcat=$category['id_category'] ; $this->pageData['products']=$this->model->products($idcat); $this->view->render($this->pageTpl,$this->pageData); } } 

and for every CategoryModel.php

 class CategoryModel extends Model { public static function Get_Category (){ $conn=DB::connToDB(); $sql="SELECT id, name FROM categories"; $result = array(); $stmt= $conn->prepare($sql); $stmt ->execute(); $i=1; while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $result[$i]['id']=$row['id']; $result[$i]['name']=$row['name']; $i++; } return $result; } } 

    1 answer 1

    You missed PDO a little, did you use placeholder in the query ? , and then they tried to bind the name parameter i which you do not have, if you want through the name parameter request should be like this:

     $stmt=$this->db->prepare("SELECT id, name,price,description FROM products WHERE category_id = :i"); $stmt->bindParam('i',$idcat); $stmt ->execute(); 

    If through a placeholder, then this:

     $stmt=$this->db->prepare("SELECT id, name,price,description FROM products WHERE category_id = ?"); $stmt ->execute(array($idcat)); 
    • one
      Oh, I probably read some wrong information that it is better not to use execute () for numbers, but with bindParam () I really missed) Thanks a lot, it helped)))) - Linetta