<?php
try{
header('Content-Type:text/html; charset=utf-8'); //网页utf8
$pdo = new PDO ('mysql:host=localhost;dbname=test', 'test', '123456'); //连接数据库
$pdo->query("set names utf8"); //数据库utf8
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT *FROM test1";
//perpare()准备要执行的sql语句,返回pdostatement对象
$stmt = $pdo->prepare($sql);
//execute()执行一条预处理语句
$res = $stmt->execute();
//fetch()从结果集中获取一行
//fecthAll()返回一个包含结果集中所有行的数组
//setFetchMode()为语句设置默认的获取模式
if($res){
while( $row = $stmt->fetch() ){
echo $row['id']."<br/>";
echo $row['name']."<br/>";
echo $row['password']."<br/>";
echo "<hr/>";
}
}
/*$rows = $stmt->fetchAll();
print_r($rows);*/
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
pdo perpare() 和 execute()函数实现select语句
最新推荐文章于 2024-04-29 14:24:25 发布