In previous articles, we have already talked about data passage in PHP 7 using PDO. You can access this content in the links below:
- https://www.hcode.com.br/blog/consulta-pdo-com-passagem-de-parametros-no-php-7-parte-1
- https://www.hcode.com.br/blog/consulta-pdo-com-passagem-de-parametros-no-php-7-parte-2
In these articles, we learned to use the bindParam() method to dynamically pass values for an SQL request using PHP. However, PHP also has the bindValue() method, which is used in the same context. You might have asked yourself: “What is the difference between them?” That is what we will learn in this article.
bindParam() vs bindValue()
In short, the difference between these methods is as follows:
1 - bindParam(). To set the value of a parameter, this method receives a reference as an argument, meaning a variable or a constant. This means that if we provide a value directly to it, it will not work.
2 - bindValue(). To set the value of a parameter, this method can receive as an argument both a reference and a direct value.
To better understand this difference, let's perform a practical example.
Example
We will use the code we have developed in other articles about PDO, which you can access at the beginning of this article. We will use PHP and PDO again to execute an INSERT script in the Database. Our code will look like this at first:
<?php
$connection = new PDO("mysql:dbname=hcode_pdo;host=127.0.0.1", "root", "root");
$statemet = $connection->prepare("INSERT INTO table_users (nameUser, emailUser) VALUES(:userName, :userEmail)");
Explaining: we created the Database connection variable and created the $statement variable, which will be responsible for preparing the SQL query that will be executed in the Database. Note that we created two parameters in this query: :userName and :userEmail. We will set the value of these parameters using the bindParam() method:
$statemet->bindParam(':userName', "Gláucio Daniel");
$statemet->bindParam(':userEmail', "[email protected]");
$statemet->execute();
echo "Data inserted!";
Explaining: we called the bindParam() method twice, we called the execute() method, which is responsible for actually executing the SQL query with the parameters in the Database and finally, we created a success message that will be displayed if everything goes smoothly.
However, still talking about the bindParam() method, it is interesting to note that we defined the parameter values as if in plain text, that is, in “plain text,” through a string. Let’s test this code in the browser.
An error was returned, informing that the bindParam() method could not receive the value of its parameter by reference. This error occurs because this method needs a reference, or variable to work. To resolve this issue, our code would need to be as follows:
$name = "Gláucio Daniel";
$email = "[email protected]";
$statemet->bindParam(':userName', $name);
$statemet->bindParam(':userEmail', $email);
$statemet->execute();
echo "Data inserted!";
Notice that we now created two variables containing the parameter values and we provided these variables to the bindParam() method. Let’s test this code again in our browser.
Now the success message was returned. Let's check if the data was indeed inserted into our Database.
Excellent, the data was inserted!
Now, let's do the same procedure with the bindValue() method. With this method, we can provide the values of the parameters in two ways: with variables or with “plain text.” See below an example with variables:
$name = "Djalma Sindeaux";
$email = "[email protected]";
$statemet->bindValue(':userName', $name);
$statemet->bindValue(':userEmail', $email);
Notice that the syntax hasn’t changed much, only the method name in fact. Let’s check if it still works in the browser.
Perfect, it continues to work. Now let’s see an example providing the parameter values directly:
$statemet->bindValue(':userName', "Ronaldo Braz");
$statemet->bindValue(':userEmail', "[email protected]");
When testing in the browser, we see the following message:
The success message was returned. But, are the new data in our Database?
Very good, the data was correctly inserted. Thus, we understand that the difference between the bindParam() and bindValue() methods is small, and one method offers a slight resource more than the other, but both work with the same objective.
Now that we understand the functionality of each of these methods, we are prepared to decide when to use them in our projects to continue developing modern and professional PHP codes.
We talked a lot in this article about passing parameters by value and by reference in PDO. But this concept is also applied when we create our own functions in PHP.
We will learn a little more about this in the next article: Passing Parameters by Value and Reference in PHP, until then! :)