$pdo->query($sql)
<?php
header("content-type:text/html;charset=utf-8");
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$sql = "select * from user where id>3;";
$stmt = $pdo->query($sql);
var_dump($stmt);
/*
* object(PDOStatement)[2]
public 'queryString' => string 'select * from user where id>3;' (length=30)
*如果错的返回false
*/
echo "<hr/>";
foreach ($stmt as $row) {
//var_dump($row);
echo "编号:" . $row['id'] . "<br/>";
echo "用户名:" . $row['username'] . "<br/>";
echo "邮箱:" . $row['email'] . "<br/>";
echo "<hr/>";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
添加
<?php
header("content-type:text/html;charset=utf-8");
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$sql = 'INSERT user(username,password,email) VALUES("king17","' . md5('king11') . '","imooc@qq.com")';
$pdo->query($sql);
$stmt = $pdo->query($sql);
var_dump($stmt);//boolean false
} catch (PDOException $e) {
echo $e->getMessage();
}
?>