Solved

Question on using Prepared Statements with PHP's PDO

Posted on 2014-01-08
2
439 Views
Last Modified: 2014-01-08
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.
0
Comment
Question by:elepil
2 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39767165
the execute() function is in the statement object, not the db object

ie

 $products = $stmt->execute();
0
 

Author Closing Comment

by:elepil
ID: 39767202
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now