Link to home
Create AccountLog in
Avatar of elepil
elepil

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of elepil
elepil

ASKER

Thanks!