Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO bindParam vs. execute

Tags:

php

pdo

I often see code using bindParam or bindValue with PDO. Is simply passing arguments to execute frowned upon for any reason?

I understand that bindParam actually binds to the variables and that you can set the type of parameter being bound with both bind methods, but what if you are only inserting strings?

$query = "SELECT col1 FROM t1 WHERE col2 = :col2 AND col3 = :col3 AND col4 = :col4"; $pdo->bindValue(':col2', 'col2'); $pdo->bindValue(':col3', 'col3'); $pdo->bindValue(':col4', 'col4'); 

I often see the above, but personally I prefer:

$pdo->execute(array(':col2' => 'col2', ':col3' => 'col3', ':col4' => 'col4')); 

It is not as verbose and visually it makes more sense to me to have the inputs "going in" to the query together. However, I hardly ever see it used.

Is there a reason to prefer the bind methods over passing parameters to execute when you don't have to take advantage of the special behaviors of the former?

like image 974
Explosion Pills Avatar asked Sep 12 '12 16:09

Explosion Pills


People also ask

What is the difference between bindValue and bindParam?

bindParam is a PHP inbuilt function used to bind a parameter to the specified variable name in a sql statement for access the database record. bindValue, on the other hand, is again a PHP inbuilt function used to bind the value of parameter to the specified variable name in sql statement.

What is Execute () in PHP?

Execute: At a later time, the application binds the values to the parameters, and the database executes the statement. The application may execute the statement as many times as it wants with different values.

What PDO execute return?

PDO::exec() executes an SQL statement in a single function call, returning the number of rows affected by the statement. PDO::exec() does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing PDO::query().

What is PDO :: Param_str in PHP?

PDO::PARAM_STR. Represents SQL character data types. For an INOUT parameter, use the bitwise OR operator to append PDO::PARAM_INPUT_OUTPUT to the type of data being bound. Set the fourth parameter, length , to the maximum expected length of the output value.


2 Answers

You might find bindParam used when you just want to bind a variable reference to a parameter in the query, but perhaps still need to do some manipulations on it and only want the value of the variable calculated at time of query execution. It also allows you to do more complex things like bind a parameter to a stored procedure call and have the returned value updated into the bound variable.

For more, see the bindParam documentation, bindValue documentation and execute documentation.

For example

$col1 = 'some_value'; $pdo->bindParam(':col1', $col1); $col1 = 'some_other_value'; $pdo->execute(); // would use 'some_other_value' for ':col1' parameter 

bindValue and passing an array to execute behave in much the same way as the parameter value is fixed at that point and SQL executed accordingly.

Following the same example above, but using bindValue

$col1 = 'some_value'; $pdo->bindValue(':col1', $col1); $col1 = 'some_other_value'; $pdo->execute(); // would use 'some_value' for ':col1' parameter 

When passing values directly in execute all values are treated as strings (even if integer value is provided). So if you need to enforce data types, you should always use bindValue or bindParam.

I think you might see bind* used more than execute(array) as many consider it to be better coding practice to explicitly define data types in parameter declarations.

like image 144
Mike Brant Avatar answered Sep 25 '22 17:09

Mike Brant


By passing the parameters along with the $pdo->execute() method, all values in the array with be passed, as PDO::PARAM_STR to the statement with the $pdo->bindParam() function.

The main difference that I can see now, is that with the $pdo->bindParam() function, you can define the data type passed along, using the PDO::PARAM_* constants as described in the PHP.net manual

like image 22
Jens Kooij Avatar answered Sep 21 '22 17:09

Jens Kooij