The difference between PHP PDOStatement object bindpram(), bindvalue() and bindcolumn, pdostatement_PHP tutorial

WBOY
Release: 2016-07-13 10:13:23
Original
1087 people have browsed it

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...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!