abstract:<?php //配置參數(shù); $dsn='mysql:host=127.0.0.1;dbname=luheng'; $username='root'; $password='root'; $pdo = new PDO($dsn,$username,$password); //檢測(cè)數(shù)據(jù)庫(kù)是否連接; try{ $pdo
<?php //配置參數(shù); $dsn='mysql:host=127.0.0.1;dbname=luheng'; $username='root'; $password='root'; $pdo = new PDO($dsn,$username,$password); //檢測(cè)數(shù)據(jù)庫(kù)是否連接; try{ $pdo; }catch(PDOException $e){ print_r($e->grtMessige()); exit; } // var_dump($pdo); // 查詢 $sql='select * from demo where id>:id'; //在數(shù)據(jù)庫(kù)中進(jìn)行預(yù)處理; $a=$pdo->prepare($sql); //傳參; $id=28; //參數(shù)綁定; $a->bindParam('id',$id,PDO::PARAM_INT); //返回結(jié)果集; $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); // } //綁定所需要的字段進(jìn)行遍歷; $a->bindColumn('name',$name); $a->bindColumn('age',$age); while ($a->fetch(PDO::FETCH_ASSOC)) { echo $name.$age.'<br>'; } $pdo=null; ?>
Correcting teacher:天蓬老師Correction time:2019-04-20 13:31:41
Teacher's summary:將結(jié)果集的列綁定到指定變量上, 是一種很自然的想法, 實(shí)際的效率也不錯(cuò)的