阅读背景:

pdo中使用bindParam和bindValue的区别_dzyweer的博客

来源:互联网 
<?php
header("Content-type: text/html; charset=utf-8");
$dbms='mysql';
$dbName='goodsdb';
$user='root';
$pwd='root';
$host='localhost';
$charset = 'utf8';
$dsn="$dbms:host=$host;dbname=$dbName;charset=$charset";
try{
    $pdo=new PDO($dsn,$user,$pwd);
}
catch(Exception $e)
{
    echo $e->getMessage();
}

//查询
$sql = "select * from stu where id=?";
//准备sql模板
$stmt = $pdo->prepare($sql);
$name = 'a';
/*
 * 绑定参数  .这里讲一下 2个方法的区别
 * bindParam是引用传递
 * bindValue是值传递
 *
 */
//例1 2种方式都支持
$id = 11;
$stmt->bindParam(1,$id);
$stmt->bindValue(1,$id);

//例2 bindParam适用 bindValue不适用
$id = 11;
$stmt->bindParam(1,$id);
//由于某种业务逻辑
$id = 3;
//这时候。我们的预期是想查询id为3的结果

//例3 bindValue适用 bindParam不适用.这里你用bindParam会报语法错误
$stmt->bindValue(1,1);

$stmt->execute();



while ($row=$stmt->fetch())
{
    print_r($row);
}
//释放查询结果
$stmt = null;
//关闭连接
$pdo = null;<?php
header("Content-type: text/html; charset=



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: