PDOStatement :: bindParam() The function is a built-in function in PHP that is used to bind parameters to a specified variable name. This function binds a variable, passes its value as input, and receives the output value of its associated argument tag, if any.
Syntax:
bool PDOStatement::bindParam ( $parameter, $variable, $data_type, $length, $driver_options )
Parameters: This function accepts five parameters as mentioned above, as described below:
$parameter: It is a parameter identifier for Prepare statements using name placeholders. It is the parameter name of the form: name.
$variable: This parameter is used to save the variable name to be bound to the SQL statement parameter.
$data_type: It is the explicit data type of the parameters using PDO::PARAM_* constants.
$length: This parameter is used to save the length of the data type.
$driver_options: This parameter saves the operations that need to be performed.
Return value: This function returns True when successful and false when failed.
Procedure:
<?php // 设置PDO连接 $db = new PDO('mysql:host=localhost;dbname=phps','root',''); // 获得用户名 $username = 'phpforphp'; $stmt = $db->prepare("SELECT * FROM users WHERE user = :username"); // 使用bindParam函数 $stmt->bindParam(':username', $username); $username = 'g4g'; $stmt->execute(); ?>
Note: The SQL statement will be executed using 'g4g' as the username because: username searches for $username at execution time and the last known value of $username was 'g4g '.
PDOStatement::bindValue() The function is a built-in function in PHP that is used to bind values to parameters. This function binds a value to the corresponding named or question mark placeholder in SQL used for prepared statements.
Syntax:
bool PDOStatement::bindValue( $parameter, $value, $data_type )
Parameters: This function accepts the above three parameters as described below:
$parameter: It is a parameter identifier used to account for prepared statement. It is the parameter name of the form: name.
$value: This parameter is used to save the value of the binding parameter.
$data_type: It is the explicit data type of the parameters using PDO::PARAM_* constants.
Return value: This function returns True when successful and False when failed.
Procedure:
<?php // 设置PDO连接 $db = new PDO('mysql:host=localhost;dbname=phps','root',''); // 获得用户名 $username = 'phpforphp'; $stmt = $db->prepare("SELECT * FROM users WHERE user = :username"); // 使用bindValue函数 $stmt->bindValue(':username', $username); $username = 'g4g'; $stmt->execute(); ?>
Note: The SQL statement will be executed using 'g4g' as the username because the literal value "phpforphp" is bound to: bindValue() function previous username. Further changes to $username will not be reflected in the prepared statement.
The difference between bindParam() and bindValue():
bindParam()
bindParam() function will Parameters are bound to named or question mark placeholders in the SQL statement.
bindParam() function is used to pass variables instead of values.
bindValue()
bindValue() function binds a value to a name or question mark in an SQL statement.
bindValue() function is used to pass values and variables.
Recommended: "PHP Tutorial"http://www.php.cn/course/list/29.html
This article This is an introduction to the difference between bindParam and bindValue in PHP. I hope it will be helpful to friends who need it!
The above is the detailed content of The difference between bindParam and bindValue in PHP. For more information, please follow other related articles on the PHP Chinese website!