bindColumn、bindParam与bindValue的区别


bindColumn:绑定一列到一个 PHP 变量(类似于list()函数为变量赋值)

<?php //连接数据库函数
 functionconnect() { try { $dbh = new PDO("mysql:host=localhost;dbname=test",'root','root'); return $dbh; } catch(Exception $e){ echo $e->getMessage(); } } // 使用bindColumn读取数据
 functionreadDataByColumn($dbh) { $sql = 'SELECT id,name FROM users'; # 读取users表中id和name字段

try { $stmt = $dbh->prepare($sql); $stmt->execute(); /* 通过列号绑定,将ID这一列的值绑定到$id这个变量上 */

    $stmt->bindColumn(1, $id); /* 通过列名绑定,将name这一列字段的值绑定到$name这个变量上 */

    $stmt->bindColumn('name', $name); while ($row = $stmt->fetch(PDO::FETCH_BOUND)) { $data = $id . "\t" . $name . "\t"; echo"<pre>"; print $data; } } catch (PDOException $e) { print $e->getMessage(); } } $dbh = connect();       // 连接数据库
 readDataByColumn($dbh); // 使用bindColumn读取数据

?>

 

运行结果如下:

1 Michael 2 Andy 3        xiaoming

 

bindParam:绑定一个参数到指定的变量名(类似于占位符)

<?php //连接数据库函数
 functionconnect() { try { $dbh = new PDO("mysql:host=localhost;dbname=test",'root','root'); return $dbh; } catch(Exception $e){ echo $e->getMessage(); } } // 使用bindColumn读取数据
 functionreadDataByParam($dbh) { $num = 10; $username = '%m%'; $sql = 'SELECT id, name FROM users WHERE id < :num and name like :username'; try { $stmt = $dbh->prepare($sql);                              // 预处理

    $stmt->bindParam(':num', $num, PDO::PARAM_INT);           // 数字类型

    $stmt->bindParam(':username', $username, PDO::PARAM_STR); // 字符串类型

    $stmt->execute();                                         // 执行SQL语句

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {           // $row是一行,使用while依次输出下一行

      $data = $row['id'] . "\t" . $row['name'] . "\t"; echo"<pre>"; print $data; } } catch (PDOException $e) { print $e->getMessage(); } } $dbh = connect();       // 连接数据库
 readDataByParam($dbh); // 使用bindColumn读取数据

?>

 

bindValue — 把一个值绑定到一个参数(与bindParam类似)

<?php //连接数据库函数
 functionconnect() { try { $dbh = new PDO("mysql:host=localhost;dbname=test",'root','root'); return $dbh; } catch(Exception $e){ echo $e->getMessage(); } } // 使用bindColumn读取数据
 functionreadDataByValue($dbh) { $num = 10; $username = '%m%'; $sql = 'SELECT id, name FROM users WHERE id < :num and name like :username'; try { $stmt = $dbh->prepare($sql);                              // 预处理

    $stmt->bindValue(':num', 10, PDO::PARAM_INT);             // 数字类型

    $stmt->bindValue(':username', '%m%', PDO::PARAM_STR);     // 字符串类型

    $stmt->execute();                                         // 执行SQL语句

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {           // $row是一行,使用while依次输出下一行

      $data = $row['id'] . "\t" . $row['name'] . "\t"; echo"<pre>"; print $data; } } catch (PDOException $e) { print $e->getMessage(); } } $dbh = connect();       // 连接数据库
 readDataByValue($dbh); // 使用bindColumn读取数据

?>

 

bindParam和bindValue的区别

  1.  PDOStatement::bindParam不能绑定常量,而bindValue可以绑定常量 如 $stm->bindParam(":sex",$sex); //正确 $stm->bindParam(":sex","female"); //错误 $stm->bindValue(":sex",$sex); //正确 $stm->bindValue(":sex","female"); //正确
  1. bindParam 变量被以引用方式绑定到点位符上,而且仅仅当调用PDOStatement::execute()时才会去计算具体被绑定变量在PDOStatement::execute()被调用时的值. 例如,使用bindParam方式:
<?php $sex = 'male'; $s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex'); $s->bindParam(':sex', $sex); // use bindParam to bind the variable

$sex = 'female'; $s->execute(); // 将执行 WHERE sex = 'female'

 

使用bindvalue方式:

<?php $sex = 'male'; $s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex'); $s->bindValue(':sex', $sex); // use bindValue to bind the variable's value

$sex = 'female'; $s->execute(); // 将执行 WHERE sex = 'male'

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM