Blogger Information
Blog 25
fans 0
comment 0
visits 18842
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
222作业2
℃级的博客
Original
708 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";

    $sql="SELECT `id`,`name`,`sex` FROM staff where `id` BETWEEN :start AND :stop ";


     $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);

 var_dump($st);


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

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


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

$st->bindColumn('name',$name,PDO::PARAM_STR,15);

$st->bindColumn('sex',$sex,PDO::PARAM_INT);//bindColum将结果集中的列绑定到指定变量中

/*$start=1;

$stop=3;*/


echo '<br>';

$st->execute(['start'=>1,'stop'=>3]);

   //echo $st->debugDumpParams();

/*   while ($row=$st->fetch(PDO::FETCH_ASSOC)) {

       echo '<br>'.print_r($row,true);

   }*/

 /*  $rows=$st->fetchAll(PDO::FETCH_ASSOC);

   foreach ($rows as $row) {


    echo '<pre>'.print_r($row,true).'<br>';

   }*/

   while ($st->fetch(PDO::FETCH_BOUND)) {

    //PDO::FETCH_BOUND 指定获取方式将结果集中的列绑定到指定变量中

       echo 'id='.$id.'name='.$name.'sex='.$sex.'<br>';

   }

 //$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