PDO查詢數據簡單示例


一般情況下,查詢用到的次數遠比增刪查要多。

那么PDO如何查詢呢?直接上代碼!!!

$atime=5;
$pdo=new PDO('mysql:host=127.0.0.1;dbname=test1','root','root');
$stmt=$pdo->prepare('select * from test where atime>:atime');
$stmt->bindParam(':atime',$atime);
$stmt->execute();

$stmt=$pdo->prepare('select * from test where atime>:atime');
$stmt->bindParam(':atime',$atime);
$stmt->execute();

//查詢單條數據
$a=$stmt->fetch(PDO::FETCH_ASSOC);//關聯數組print_r($a);
$a=$stmt->fetch(PDO::FETCH_LAZY);//只能用於單條查詢
//PDORow Object
//(
//      [queryString] => select * from test where atime>:atime
//      [id] => 7
//      [aname] => aa
//      [atime] => 23
//)

//查詢多條數據
$a=$stmt->fetchAll(PDO::FETCH_ASSOC);//關聯數組
//索引鍵
$a=$stmt->fetchAll(PDO::FETCH_NUM);//索引鍵
//既有索引鍵也有關聯鍵
$a=$stmt->fetchAll(PDO::FETCH_BOTH);//既有索引鍵也有關聯鍵

Array
//(
//    [0] => Array
//    (
//        [id] => 7
//            [0] => 7
//            [aname] => aa
//[1] => aa
//[atime] => 23
//            [2] => 23
//        )
//
//    [1] => Array
//(
//    [id] => 8
//            [0] => 8
//            [aname] => aa
//[1] => aa
//[atime] => 23
//            [2] => 23
//        )

  嗯,就是如此簡單~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM