Now that PHP has true object-oriented capabilities, it's best practice to access databases using PDO (PHP Data Objects) and MySQLi. These methods produce database-neutral code that works with over a dozen systems, including MySQL, SQL Server, PostgreSQL, and SQLite. Learn how to use PDO and MySQLi to perform basic select, insert, update, and delete operations; improve security with prepared statements; and use transactions to execute multiple queries simultaneously. Author David Powers also covers advanced topics like instantiating custom objects, and compares PDO to MySQLi so you can decide which method is right for you.

Passing an array of values to the execute() method

In addition to binding values to placeholderswith the bindParam and bindValue methods, PDOallows you to pass the values directly to the execute method as an array.When you do this, all values are treated as strings.You can either define the array first or createit directly between the parentheses of the execute method.We'll look at both ways.This is pdo_named_excute.php, which you can find in thechapter three 03_04 folder of the exercise files.

It's exactly the same file as the one we created earlier using named parameters.Because the parameters have names, you need to create anassociative array using the name as the key for each value.Because they're individually named, they can be in any order.So, let's create a new array.We'll create it after preparing the statement.We'll call the array values and just becausewe can, let's put them in a different order.

So, we'll start off with colon price.And the double arrow operator for the value.That comes directly from the get array, from get price.And a comma and our next value.This time we'll have :make.This needs to begin with the wild cardcharacter concatenated with the value from the get array.And another wildcard character, and then our final array element willbe yearmade, followed by its value.Now that we've created this array we don't need to bind the valuesusing bindValue or bindParam so we can delete lines 14, 15 and 16, andthen just pass the values array directly to the execute method.

So, if we save that and run it in a browser, it should work exactly as before.So let's see if we can find some, I don't know, fords.Here we are.It's working exactly as you would expect.Now we did put price in the wrong order.So let's just test that again.We've got a Ford here that is worth 10,980.So, if we look for fords again.And set the maximum price to 10,000, this one should not be displayed.

And it's working exactly as we would like it to do.And the reason for that, of course, is because we do have named parameters.And the names are identified in the associativearray so it doesn't matter which order they're in.So, let's see how it works with anonymous placeholders.This is pdo_anonymous_execute.php, which you can findin the same chapter three, 03_04 folder of the exercise files.It's the same file that we used before with anonymous placeholders.Because there are known names with an anonymousplaceholder, you create an indexed array without keys.

So the values this time must be in exactly the same order as the placeholders.That means we need to have make, yearmade and price in that order.Let's do it directly between the parentheses ofthe execute method so we can get rid ofthe bindValue and bindParam lines and then we'll createan anonymous array directly inside there between the parentheses.The first value needs to be make.So that begins with the placeholder character,concatenated with the value from the GET array,and concatenated with another placeholder character.Then, a comma for the next array element.This is going to be yearmade.That also comes from the GET array, we just go to it directly.And the final value in that array needs to be price.

So, if we save that and load this page in abrowser, it should work exactly the same as the other pages.So, we'll look for BMWs.There it is, it's working perfectly.Passing the values directly as an array to the execute method is a convenientshorthand, but it does have the disadvantagethat all values are passed as strings.That means you can't insert a null value.With anonymous parameters, there's also a danger of getting them inthe wrong order if you make any changes to the SQL.

However, it is worth considering passing the values directly to theexecute method if the prepared statement takes only a single input value.But with a single value, the argument passed tothe execute method still needs to be an array.Just make sure it's an array with a single element.

There are currently no FAQs about Accessing Databases with Object-Oriented PHP.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.