PDOStatement::bindParam 、bindValue、bindColumn实例比较

来源:互联网 发布:光盘直接复制数据 编辑:程序博客网 时间:2024/05/16 08:28

PDOStatement::bindParam — 》绑定一个参数到指定的变量名。

bindParam问号占位符

<?phpheader("content-type:text/html;charset=utf-8");try{    $pdo=new PDO('mysql:host=localhost;dbname=test','root','root');    $sql="insert into user(username,password,email) values (?,?,?)";    $stmt=$pdo->prepare($sql);    $stmt->bindParam(1,$username);//参数索引位置,从一开始    $stmt->bindParam(2,$password);    $stmt->bindParam(3,$email);    $username='imooc1';    $password='imooc1';    $email='imooc1@imooc.com';    $stmt->execute();}catch(PDOException $e){    echo $e->getMessage();}?>
bindParam命名占位符
<?phpheader("content-type:text/html;charset=utf-8");try{    $pdo=new PDO('mysql:host=localhost;dbname=test','root','root');    $sql="insert into user(username,password,email) values (:username,:password,:email)";    $stmt=$pdo->prepare($sql);    $stmt->bindParam(':username',$username,PDO::PARAM_STR);    $stmt->bindParam(':password',$password,PDO::PARAM_STR);    $stmt->bindParam(':email',$email,PDO::PARAM_STR);    $username='imooc2';    $password='imooc2';    $email='imooc2@imooc.com';    $stmt->execute();}catch(PDOException $e){    echo $e->getMessage();}?>
PDOStatement::bindValue —》 把一个值绑定到一个参数。

bindValue问号占位符

<?phptry{    $pdo=new PDO('mysql:host=localhost;dbname=test','root','root');    $sql="insert into user(username,password,email) values(?,?,?)";    $stmt=$pdo->prepare($sql);    $stmt->bindValue(1,'imooc3');    $stmt->bindValue(2,'imooc3');    $stmt->bindValue(3,'imooc@imooc.com');    $stmt->execute();    $username='imooc4';    $password='imooc4';    $stmt->bindValue(1,$username);    $stmt->bindValue(2,$password);                 //对于email一样,可以一次绑定,多次使用    $stmt->execute();}catch(PDOException $e){    echo $e->getMessage();}?>
bindValue命名占位符
<?phptry{    $pdo=new PDO('mysql:host=localhost;dbname=test','root','root');    $sql="insert into user(username,password,email) values(:username,:password,:email)";    $stmt=$pdo->prepare($sql);    $stmt->bindValue(':username','imooc33');    $stmt->bindValue(':password','imooc33');    $stmt->bindValue('email','imooc@imooc.com');    $stmt->execute();    $username='imooc44';    $password='imooc44';    $stmt->bindValue(':username',$username);    $stmt->bindValue(':password',$password);    //对于email一样,可以一次绑定,多次使用    $stmt->execute();}catch(PDOException $e){    echo $e->getMessage();}?>
PDOStatement::bindColumn—》 绑定一列到php变量
<?phpheader("content-type:text/html;charset=utf-8");try{    $pdo=new PDO('mysql:host=localhost;dbname=test','root','root');     $sql='select username,password,email from  user';     $stmt=$pdo->prepare($sql);     $stmt->execute();     echo '结果集中的列数有'.$stmt->columnCount().'列'.'<br>';     $stmt->bindColumn(1,$username);     $stmt->bindColumn(2,$password);     $stmt->bindColumn(3,$email);     while($stmt->fetch(PDO::FETCH_BOUND)){         echo '用户名:'.$username.'密码:'.$password.'邮件:'.$email;         echo "<br>";     }}catch(PDOException $e){    echo $e->getMessage();}?>