夺命雷公狗---PDO NO:11 PDO的预处理查询2

<?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));
    //通过索引数组的方法返回
    $stmt -> setFetchMode(PDO::FETCH_NUM);
    echo “<pre>”;
    print_r($stmt -> fetchall());
    echo “</pre>”;
}catch(PDOException $e){
    echo “错误:”.$e -> getMessage();
}

 

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