The difference between bindParam and bindValue in PHP

藏色散人
Release: 2023-04-05 11:32:02
Original
3717 people have browsed it

The difference between bindParam and bindValue in PHP

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 )
Copy after login

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(&#39;mysql:host=localhost;dbname=phps&#39;,&#39;root&#39;,&#39;&#39;);  
  
// 获得用户名
$username = &#39;phpforphp&#39;; 
     
$stmt = $db->prepare("SELECT * FROM users WHERE user = :username"); 
  
// 使用bindParam函数
$stmt->bindParam(&#39;:username&#39;, $username); 
   
 $username = &#39;g4g&#39;; 
     
 $stmt->execute(); 
?>
Copy after login

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 )
Copy after login

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(&#39;mysql:host=localhost;dbname=phps&#39;,&#39;root&#39;,&#39;&#39;);  
  
// 获得用户名
$username = &#39;phpforphp&#39;; 
    
$stmt = $db->prepare("SELECT * FROM users WHERE user = :username"); 
  
// 使用bindValue函数
$stmt->bindValue(&#39;:username&#39;, $username); 
  
$username = &#39;g4g&#39;; 
    
$stmt->execute(); 
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template