What's the difference between PDOStatement->bindParam() and PDOStatement->bindValue()? [duplicate]

Asked
Active3 hr before
Viewed126 times

5 Answers

duplicatedifference
90%

What is the difference between PDOStatement::bindParam() and PDOStatement::bindValue()?,[With bindParam] Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.,bindParam works only with variables because it allows parameters to be given as input/output, by "reference" (and a value is not a valid "reference" in PHP) : it is useful with drivers that (quoting the manual) : ,Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

So, for example:

$sex = 'male';
$s = $dbh - > prepare('SELECT name FROM students WHERE sex = :sex');
$s - > bindParam(':sex', $sex); // use bindParam to bind the variable
$sex = 'female';
$s - > execute(); // executed with WHERE sex = 'female'

or

$sex = 'male';
$s = $dbh - > prepare('SELECT name FROM students WHERE sex = :sex');
$s - > bindValue(':sex', $sex); // use bindValue to bind the variable's value
$sex = 'female';
$s - > execute(); // executed with WHERE sex = 'male'
load more v
88%

PDOStatement::bindValue() - Binds a value to a parameter,PDOStatement::bindParam — Binds a parameter to the specified variable name , Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called. ,Example #1 Execute a prepared statement with named placeholders

load more v
72%

Return Value: This function returns True on success or false on failure.Program:  ,Return Value: This function returns True on success or False on failure.Program:,PDOStatement::bindParam() Function,PDOStatement::bindValue() Function

Syntax:

bool PDOStatement::bindParam($parameter, $variable, $data_type, $length, $driver_options)

Syntax:

bool PDOStatement::bindValue($parameter, $value, $data_type)
load more v
65%

What's the difference between PDOStatement :: bindParam () and PDOStatement :: bindValue ()? , With bindParam , unlike bindValue , the variable   is linked to a reference and will only be assessed at the time   PDOStatement :: execute () is called. , No bindParam() the expected argument is a reference (variable or constant) can not be a primitive type such as a string or loose number, function / method return. bindValue() can receive references and values as argument, basically that's it. , In bindParam () the expected argument is a reference (variable or constant) and can not be a primitive type such as a string or loose number, function / method return. Already bindValue () can get references and values as arguments.

  

In bindParam () the expected argument is a reference (variable or constant) and can not be a primitive type such as a string or loose number, function / method return. Already bindValue () can get references and values as arguments.

$stmt - > bindParam(':v1', 10); // Inválido
$stmt - > bindParam(':v1', getValor()); // Inválido

With bindParam:

$sex = 'male';
$s = $dbh - > prepare('SELECT name FROM students WHERE sex = :sex');
$s - > bindParam(':sex', $sex);
$sex = 'female';
$s - > execute(); // Executado quando $sex = 'female'

With bindValue:

$sex = 'male';
$s = $dbh - > prepare('SELECT name FROM students WHERE sex = :sex');
$s - > bindValue(':sex', $sex);
$sex = 'female';
$s - > execute(); // Executado quando $sex = 'male'
load more v
75%

The PDOStatement object that this command is associated with,The DB connection that this command is associated with,The dependency to be associated with the cached query result for this command,Returns the SQL statement for this command.

For example,

$users = $connection - > createCommand('SELECT * FROM user') - > queryAll();

Command also supports building SQL statements by providing methods such as insert(), update(), etc. For example, the following code will create and execute an INSERT SQL statement:

$connection - > createCommand() - > insert('user', [
   'name' => 'Sam',
   'age' => 30,
]) - > execute();
load more v

Other "duplicate-difference" queries related to "What's the difference between PDOStatement->bindParam() and PDOStatement->bindValue()? [duplicate]"