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.
Before diving into the details of PDO prepared statements, let's take a quick look at the different methods for binding input and output values. Prepared statements use placeholders for values that aren't known in advance or that come from external sources such as an online form. All the examples in this chapter are select queries, but you can use placeholders in prepared statements for insert, update, and delete as well. You bind values to placeholders using the bindParam or bindValue methods.
I'll explain the difference in a moment. Alternatively, you can parse an array of values directly to the execute method. A prepared statement can also bind the output values of a result set to named variables using the find column method. This avoids the need to use an associative array, simplifying embedding the results in HTML. When binding input values to placeholders, bindParam and bindValue achieve the same result but there's a subtle and perhaps confusing difference.
bindParam works only with a variable, you can't pass it an expression such as a calculation or a string concatenated to a variable. This is because the value is evaluated Until the statement is executed. In other words, you're binding the variable rather than it's value. If you attempt to use bindParam with anything other than a variable, you'll get this cryptic fatal error message that you can't pass parameter two by reference.
The solution in such circumstances is to use bindValue, which binds the value immediately, so the value must be known. BindValue is also useful for setting a field to NULL. For example, this is how you would set a named parameter called ID to NULL. The first argument is a string containing the name parameter. The second is the NULL value and the third argument is a PDO constant for the NULL data type. BindValue can be used with a variable, as long as it's value is already set.
However, you should normally use bind value only when you need to use an actual value or to combine a variable with an expression such as a calculation or as part of a string. For a variable on its own, always use bindParam. For a complete list of the PDO Constants that can be used with bindParam and bindValue, see the PHP online documentation at this location.
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.