bindColumn、bindParam与bindValue的区别
2018-01-23 10:39
507 查看
bindColumn:绑定一列到一个 PHP 变量(类似于list()函数为变量赋值)
运行结果如下:
bindParam:绑定一个参数到指定的变量名(类似于占位符)
bindValue — 把一个值绑定到一个参数(与bindParam类似)
bindParam和bindValue的区别
PDOStatement::bindParam不能绑定常量,而bindValue可以绑定常量 如 $stm->bindParam(":sex",$sex); //正确 $stm->bindParam(":sex","female"); //错误 $stm->bindValue(":sex",$sex); //正确 $stm->bindValue(":sex","female"); //正确
bindParam 变量被以引用方式绑定到点位符上,而且仅仅当调用PDOStatement::execute()时才会去计算具体被绑定变量在PDOStatement::execute()被调用时的值. 例如,使用bindParam方式:
使用bindvalue方式:
<?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的区别
PDOStatement::bindParam不能绑定常量,而bindValue可以绑定常量 如 $stm->bindParam(":sex",$sex); //正确 $stm->bindParam(":sex","female"); //错误 $stm->bindValue(":sex",$sex); //正确 $stm->bindValue(":sex","female"); //正确
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'
相关文章推荐
- bindColumn、bindParam与bindValue的区别
- php pdo中bindParam() 和 bindValue()方法的区别
- php中bindValue 和 bindParam 的区别
- pdo中bindParam()与bindValue的区别
- php pdo中PDOStatement 类的bindParam和bindValue方法的区别
- pdo中使用bindParam和bindValue的区别
- mysql pdo bindValue和bindparam的区别
- PDOStatement::bindParam 、bindValue、bindColumn实例比较
- PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
- php的PDO类中bindValue和bindParam的区别
- bindParam和bindValue的区别以及在Yii2中的使用
- PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
- PDO的bindParam与bindValue的区别
- php的PDO类中bindValue和bindParam的区别
- bindParam和bindValue的区别以及在Yii2中的使用详解
- php的PDO类中bindValue和bindParam的区别
- /doreg.jsp (line: 11, column: 0) Expecting "jsp:param" standard action with "name" and "value" attributes 错误解决办法
- hibernate错误:ORA-01461: can bind a LONG value only for insert into a LONG column
- PHP扩展PDO MySQL之PDOStatement::bindParam vs bindValue
- @PathVariable和@RequestParam的区别,理解 @PathVariable 、@RequestHeader、@CookieValue、@RequestParam, @Request