<?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 < ?”);
$stmt -> execute(array(’10’,’50’));
//绑定栏目
$stmt -> bindColumn(1, $id);
$stmt -> bindColumn(2, $name);
$stmt -> bindColumn(3, $age);
//可以设置结果的模式, 以下的代码使用fetch()或fetchAll()都是使用这个方面设置的数组的格式
$stmt -> setFetchMode(PDO::FETCH_NUM);
//mysql_fetch_array() mysql_fetch_rows mysql_fetch_array()
echo “<table border=’1′ width=’600′>”;
while($stmt -> fetch()){
echo “<tr>”;
echo “<td>id:{$id}</td>”;
echo “<td>name:{$name}</td>”;
echo “<td>age:{$age}</td>”;
echo “</tr>”;
}
echo “</table>”;
}catch(PDOException $e){
echo “错误:”.$e -> getMessage();
}