This article describes the differences between the PHPPDOStatement object bindpram (), bindvalue (), and bindcolumn. For more information, see
PDOStatement: bindParam-bind a parameter to the specified variable name.
Bind a PHP variable to the corresponding name placeholder or question mark placeholder in the SQL statement used for preprocessing. Unlike PDOStatement: bindValue (), this variable is bound as a reference and takes its value only when PDOStatement: execute () is called.
PDOStatement: bindValue-binds a value to a parameter.
Bind a value to the corresponding name placeholder or question mark placeholder in the SQL statement used for preprocessing.
The code is as follows:
<? Php
$ Stm = $ pdo-> prepare ("select * from users where user =: user ");
$ User = "jack ";
// Correct
$ Stm-> bindParam (": user", $ user );
// Error
$ Stm-> bindParam (": user", "jack ");
// Correct
$ Stm-> bindValue (": user", $ user );
// Correct
$ Stm-> bindValue (": user", "jack ");
// Therefore, bindParam is the second parameter. only the variable name can be used, but not the variable value. bindValue can use the specific value.
?>
PDOStatement: bindColumn-bind a column to a PHP variable.
Bind a specific variable to a specified column in a query result set. Every call to PDOStatement: fetch () or PDOStatement: fetchAll () will update all the variables bound to the column.
The code is as follows:
<? Php
Function readData ($ dbh ){
$ SQL = 'SELECT name, color, calories FROM fruit ';
Try {
$ Stmt = $ dbh-> prepare ($ SQL );
$ Stmt-> execute ();
/* Bind the column number */
$ Stmt-> bindColumn (1, $ name );
$ Stmt-> bindColumn (2, $ color );
/* Bind by column name */
$ Stmt-> bindColumn ('calories ', $ cals );
While ($ row = $ stmt-> fetch (PDO: FETCH_BOUND )){
$ Data = $ name. "\ t". $ color. "\ t". $ cals. "\ n ";
Print $ data;
}
}
Catch (PDOException $ e ){
Print $ e-> getMessage ();
}
}
ReadData ($ dbh );
?>