夺命雷公狗---PDO NO:10 PDO的预处理查询1

<?php
header(“Content-Type:text/html;charset=utf-8″);
try{
    $pdo = new PDO(“mysql:host=localhost;dbname=xsphp”,’root’,”);
    $pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo -> query(‘set names utf8′);
}catch(PDOException $e){
    echo “数据库连接失败:”.$e -> getMessage();
    exit;
}
try{
    //自定义的查询语句格式
    $stmt = $pdo ->prepare(“select id,name,age from stu where id > ? and id < ?”);
    //要查询的id
    $stmt -> execute(array(5,10));
    //查询出来的结果可以遍历出来
    foreach($stmt as $v){
    print_r($v);
    echo “<br/>”;
    }
}catch(PDOException $e){
    echo “错误:”.$e -> getMessage();
}

 

posted @ 2015-12-09 14:49  夺命雷公狗  阅读(141)  评论(0编辑  收藏  举报