夺命雷公狗---PDO NO:13 PDO的预处理查询4

<?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 > 5 and id <50″);
    //这里是通过?号进行传参
    $stmt -> execute(array(5,50));
    //bindcolumn是用来版定的  由于是通过关联数组方式进行版定所以不用担心顺序问题
    $stmt -> bindColumn(“id”,$id);
    $stmt -> bindColumn(“name”,$name);
    $stmt -> bindColumn(‘age’,$age);
    $stmt -> setFetchMode(PDO::FETCH_NUM);
    echo “<table border=”1>”;
    //fetch()是他自带的一个方法
while($stmt -> fetch()){
    echo “<tr>”;
    echo “<td>id:{$id}</td>”;
    echo “<td>name:{$name}</td>”;
    echo “<td>age:{$age}</td>”;
    echo “</tr>”;
}
    
}catch(PDOException $e){
    echo “错误:”.$e -> getMessage();
}

 

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