1. PDO是什么
2. PDO怎么开启和测试开启
3. 基本操作
<?php
//echo phpinfo(); check open pdo
//单态类(目的是为了产生唯一的对象)
// class A{
// private static $a = null;
// private function __construct(){
// }
// static function makeA(){
// if(self::$a == null){
// self::$a = new self();
// }
// return self::$a;
// }
// }
// print_r(A::makeA());
//--------------------
//$mysqli = new mysqli("localhost","user","password","dbname");
// try{
//pdo connect method.
//1.$pdo = new PDO("mysql:host=localhost;dbname=test","root","");
//2.$pdo = new PDO("uri:mysqlPdo.ini","root","");
//$pdo = new PDO("mysqlpdo","root",""); //php.ini
// $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
// }catch(PDOException $e){
// die("database connect fail".$e->getMessage());
// }
//echo $pdo->getAttribute(PDO::ATTR_CLIENT_VERSION);
// echo $pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);
//print_r($pdo);
//----------------
//query($sql) exec($sql) setAttribute() fetchAll()
//1. connect the database
try{
$pdo = new PDO("mysql:host=localhost;dbname=test;","root","");
}catch(PDOException $e){
die("database connect fail".$e->getMessage());
}
// //2. exect query()function
// $sql = "select * from users";
// $stmt = $pdo->query($sql);
// $list = $stmt->fetchAll(PDO::FETCH_ASSOC);
// //print_r($list);
// //3.jiexishuju
// foreach($list as $val){
// echo $val[‘id‘]."----".$val[‘name‘]."<br>";
// }
// //4. 释放资源
// $stmt = null;
// $pdo = null;
//2. select
// $sql = "select * from users";
// foreach($pdo->query($sql) as $val){
// echo $val[‘id‘]."---".$val[‘name‘]."<br>";
// }
//3. insert
//$sql = "insert into users values(null,‘meng‘,‘40‘)"; //insert
//$sql = "delete from users where id=6"; //delete
$sql = "update users set name=‘wang‘ where id=3"; //update
$res = $pdo->exec($sql);
if($res){
echo "success";
}