Viewers: in countries Watching now:
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.
We've just seen how to use named parameters in a PDO prepared statement. You can also use question marks as placeholders, or anonymous parameters. Continue working with the file from the previous video. Alternatively, open pdo_anonymous.php, which you can find in the chapter three 03_03 folder of the Exercise Files. The where clause in the SQL uses named parameters, which all begin with a colon. There are three of them. :make, :yearmade and :price.
Anonymous parameters use a question mark as the placeholder. So to replace these with anonymous placeholders, just change :make to a question mark, and do the same with the two other named parameters. Because they're anonymous, you can't use the name as the first argument to the bindValue or bindParam methods. What you need to do is to use the position that the parameter has inside the SQL counting from one. So make is one, yearmade is two and price is three.
So we just change those values down here to one, two, three. And that's all there is to it. All other aspects of using a prepared statement remain the same. You put the parameters inside the SQL. You prepare the statement using the prepare method, bind the values, execute, and then fetch. So if we save that and test it in the browser, it should work exactly the same as before. So let's see if we can find some BMW's. There we are, it's working fine. Using anonymous parameters with a PDO prepared statement is very similar to using named parameters.
Just use a question mark as the parameter. You bind the values to each one using its position, counting from one in the prepared statement, as the first argument, to bindValue or bindParam. As with named parameters, the placeholder must not be enclosed in quotes. The prepared statement automatically adds quotes where necessary when the statement is executed. The only real drawback with using anonymous parameters is that you might need to renumber the bindings if you make changes to the SQL.
With named parameters, the order doesn't matter, because each column and its value are identified explicitly.
There are currently no FAQs about Accessing Databases with Object-Oriented PHP.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.