<?php //配置参数; $dsn='mysql:host=127.0.0.1;dbname=luheng'; $username='root'; $password='root'; $pdo = new PDO($dsn,$username,$password); //检测数据库是否连接; try{ $pdo; }catch(PDOException $e){ print_r($e->grtMessige()); exit; } // var_dump($pdo); // 查询 $sql='select * from demo where id>:id'; //在数据库中进行预处理; $a=$pdo->prepare($sql); //传参; $id=28; //参数绑定; $a->bindParam('id',$id,PDO::PARAM_INT); //返回结果集; $a->execute(); // echo '<pre>', print_r($resurt=$a->fetch(PDO::FETCH_ASSOC),true); // while+fetch查询; // while ($resurt=$a->fetch(PDO::FETCH_ASSOC)) { // echo '<pre>'.print_r($resurt,true); // } // foreach+fetchALL查询; // $resurt=$a->fetchALL(PDO::FETCH_ASSOC); // foreach ($resurt as $val) { // echo '<pre>'.print_r($val,true); // } //绑定所需要的字段进行遍历; $a->bindColumn('name',$name); $a->bindColumn('age',$age); while ($a->fetch(PDO::FETCH_ASSOC)) { echo $name.$age.'<br>'; } $pdo=null; ?>