PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间
《PHP实例:PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别》要点: PHP利用PDOStatement::bindParam ― 绑定一个参数到指定的变量名. 绑定一个PHP变量到用作预处理的SQL语句中的对应命名占位符或问号占位符. 分歧于 PDOStatement::bindValue(),此变量作为引用被绑定,并只在 PDOStatement::execute() 被调用的时候才取其值. PHP利用PDOStatement::bindValue ― 把一个值绑定到一个参数. 绑定一个值到用作预处置的 SQL 语句中的对应命名占位符或问号占位符.
代码如下:
<?php $stm = $pdo->prepare("select * from users where user = :user"); $user = "jack"; //正确 $stm->bindParam(":user",$user); //错误 $stm->bindParam(":user","jack"); //正确 $stm->bindValue(":user",$user); //正确 $stm->bindValue(":user","jack"); ? //所以使用bindParam是第二个参数只能用变量名,而不克不及用变量值,而bindValue至可以使用具体值. ?> PDOStatement::bindColumn ― 绑定一列到一个 PHP 变量. 支配一个特定的变量绑定到一个查询结果集中给定的列.每次调用 PDOStatement::fetch() 或 PDOStatement::fetchAll() 都将更新所有绑定到列的变量.
代码如下:
<?php function? readData ( $dbh ) { ????$sql? =? 'SELECT name,colour,calories FROM fruit' ; ????try { ????????$stmt? =? $dbh -> prepare ( $sql ); ????????$stmt -> execute (); ? ????????/*? 经由过程列号绑定? */ ????????$stmt -> bindColumn ( 1,? $name ); ????????$stmt -> bindColumn ( 2,? $colour ); ? ????????/*? 经由过程列名绑定? */ ????????$stmt -> bindColumn ( 'calories',? $cals ); ? ????????while ( $row? =? $stmt -> fetch ( PDO :: FETCH_BOUND )) { ????????????$data? =? $name? .? "t"? .? $colour? .? "t"? .? $cals? .? "n" ; ????????????print? $data ; ????????} ????} ????catch ( PDOException $e ) { ????????print? $e -> getMessage (); ????} } readData ( $dbh ); ?> 《PHP实例:PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别》是否对您有启发,欢迎查看更多与《PHP实例:PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别》相关教程,学精学透。编程之家 52php.cn为您提供精彩教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |