Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101341 Viewers
61 Video lessons · 88099 Viewers
71 Video lessons · 71950 Viewers
56 Video lessons · 103769 Viewers
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.
Your file was successfully uploaded.