Blogger Information
Blog 25
fans 0
comment 0
visits 18848
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
222作业1
℃级的博客
Original
673 people have browsed it

<?php

//1.创建PDO对象


     $sql="SELECT * FROM `staff` WHERE `id`=:id ";//sql语句模板

    // $sql = "SELECT * FROM `staff` WHERE `id`= :id ";

    // $sql="select * from staff where id=:id";


     $dsn='mysql:host=127.0.0.1;dbname=test';

     $username='root';

     $passod='root';

    // $pdo=new PDO($dsn,$username,$passod);

   try {

    $pdo=new PDO('mysql:host=127.0.0.1;dbname=test','root','root');

     

    } catch (PDOException $e) {

     exit('PDO´´½¨²»³É¹¦'.$e->getMessage());

     

    }  

   // $c=$pdo->query($sql);

  //  var_dump($c);

   /*  foreach ($c as $row) {

        echo $row['name'].'=>'.$row['position'].'<br>';

     }*/



  //2.调用 prepare()创建预处理对象

 $st=$pdo->prepare($sql);

 $id=3;

$st->bindParam('id',$id,PDO::PARAM_INT);

 //PDO::PARAM_INT,整数;定义PDO模板参数数据类型,默认为字符串。

//$st->bindValue('id',$id,PDO::PARAM_INT);//bindvalue不支持$id动态设置,bindParam支持$id动态设置

$st->execute();

$result=$st->fetch(PDO::FETCH_ASSOC);

print_r($result);

 //查看预处理对象

 //var_dump($st);

 echo "<hr>";

 $id=2;

 $st->execute();

 $result=$st->fetch(PDO::FETCH_ASSOC);

 print_r($result);

 //3.关闭PDO对象

     $pdo=null;


?>


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>



</body>

</html>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post