The difference between bindpram(), bindvalue() and bindcolumn of PHP PDOStatement objects, pdostatement
PDOStatement::bindParam — Bind a parameter to the specified variable name.
Bind a PHP variable to the corresponding named placeholder or question mark placeholder in the SQL statement used for preprocessing. Unlike PDOStatement::bindValue(), this variable is bound as a reference and only takes its value when PDOStatement::execute() is called.
PDOStatement::bindValue — Bind a value to a parameter.
Binds a value to the corresponding named placeholder or question mark placeholder in the SQL statement used for preprocessing.
Copy code The code is as follows:
$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");
//So when using bindParam, the second parameter can only use variable names, not variable values, while bindValue can only use specific values.
?>
PDOStatement::bindColumn — Bind a column to a PHP variable.
Assemble a specific variable to be bound to a given column in a query result set. Each call to PDOStatement::fetch() or PDOStatement::fetchAll() will update all variables bound to the column.
Copy code The code is as follows:
function readData ( $dbh ) {
$sql = 'SELECT name, colour, calories FROM fruit' ;
Try {
$stmt = $dbh -> prepare ($sql);
$stmt -> execute ();
/* Bind by column number */
$stmt -> bindColumn ( 1 , $name );
$stmt -> bindColumn (2, $colour);
Binding through column names */
$stmt -> bindColumn ( 'calories' , $cals );
while ( $row = $stmt -> fetch ( PDO :: FETCH_BOUND )) {
$data = $name . "t" . $colour . "t" . $cals . "n" ;
Print $data ;
}
}
catch (PDOException $e) {
print of of of course
}
}
readData ( $dbh );
?>
http://www.bkjia.com/PHPjc/915441.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/915441.htmlTechArticleThe difference between PHP PDOStatement object bindpram(), bindvalue() and bindcolumn, pdostatement PDOStatement::bindParam — Binding Sets a parameter to the specified variable name. Bind a PHP variable...