Question on using Prepared Statements with PHP's PDO

Here is the problem code:

                $categoryID = 1;
                $productCode = 's%';
               
                $db = new PDO('mysql:host=localhost;dbname=my_database',
                                  'username', 'password');
                   
                    $stmt = $db->prepare("SELECT * FROM products WHERE categoryID = :categoryID, AND productCode like :productCode");
                    $stmt->bindParam(':categoryID', $categoryID, PDO::PARAM_INT);
                    $stmt->bindParam(':productCode', $productCode, PDO::PARAM_STR);

                    $products = $db->execute();

When I run the above code, I keep getting an error saying "Fatal error: Call to undefined method PDO::execute()". Yet, when I google sample code for using prepared statements with PDO, they all use the execute() method.

Can someone tell me what's wrong, please? Thanks.
elepilAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RobConnect With a Mentor Owner (Aidellio)Commented:
the execute() function is in the statement object, not the db object

ie

 $products = $stmt->execute();
0
 
elepilAuthor Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.