首先看下PDO支持的数据库引擎
Cubrid
FreeTDS / Microsoft SQL Server / Sybase
Firebird/Interbase 6
IBM DB2
IBM Informix Dynamic Server
MySQL 3.x/4.x/5.x
Oracle Call Interface
ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
PostgreSQL
SQLite 3 及 SQLite 2
Microsoft SQL Server / SQL Azure
4D
那么好!我们就先来熟悉下PDO
首先要开启php对PDO的拓展
在php.ini下
extension=php_pdo.dll开启后重启apache
extension=php_pdo_mysql.dll
查看phpinfo()
是否存在PDO module
try{ $dbh = new PDO('mysql:dbname=ceshi;host=127.0.0.1;port=3306;charset=utf8','root',''); //查询 // $query=$dbh->query('SELECT * from goods limit 0,1'); // $query->setFetchMode(PDO::FETCH_ASSOC); // $rs = $query->fetchAll(); // echo_r($rs); //插入 // echo $dbh->exec('insert into user (id,uname,price) values (3,"whr_110",200 )'); // exit(); //删除 // echo $dbh->exec('delete from user where id=3'); // exit(); }catch(PDOException $e){ echo_r($e); }
//事务 try { $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //开启事务 $dbh->beginTransaction(); $price=500; echo'<br>'. $dbh->exec("update user set price=price+{$price} where id=1"); foreach ($dbh->query('SELECT * from user WHERE id=2') as $row) { // print_r($row); //你可以用 echo($GLOBAL); 来看到这些值 if($row['price']<$price){ die(); } } echo'<br>'. $dbh->exec("update user set price=price-{$price} where id=2"); //提交事务 $dbh->commit(); } catch (PDOException $e) { //回滚事务 $dbh->rollBack(); echo "Failed: " . $e->getMessage(); } //关闭PDO $dbh=null;
以上操作就是PDO对数据库的增删改查操作!
模拟银行转账