<?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;
pdo中使用bindParam和bindValue的区别
最新推荐文章于 2023-05-17 22:42:02 发布