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