Home >Backend Development >PHP Tutorial >Operations related to PDO data access abstraction layer in PHP
PDO: Data Access Abstraction Layer
has three major characteristics:
1. Can access other databases. All databases can
2. Has transaction functions
3. With prepared statement function (to prevent SQL injection attacks)
The example operation code is as follows:
//1.Create PDO object
$dsn ="mysql:dbname=mydb;host=localhost";//数据库类型:dbname=数据库名称;host=链接的ip或本机 $pdo =new PDO($dsn,"root","root");//$dsn,帐号,密码
//2.Write SQL statement
$sql ="select * from info"; $sql ="insert into info values('004','王六','男','n007','1994-02-11')";
//3.Execute SQL statement
$stm = $pdo->query($sql); //查询语句用query,返回的是结果 $arr = $pdo->exec($sql);//增删改用exec,返回的是执行的行数
//4. Read data from the PDOStatement object
$arr =$stm->fetch(PDO::FETCH_NUM);//默认不选为PDO::FETCH_BOTH fetch为选择一条数据 $arr = $stm->fetchAll(PDO::FETCH_BOTH);//fetchAll为全选
//Transaction type: that is, either all pass or all fail. You can refer to Taobao shopping. The deduction and deduction must be met at the same time. There are three conditions for destocking and adding orders, one of which is indispensable
//beginTransation Start transaction
//commit Submit transaction
//rollback Rollback: return to before starting transaction
The above is the detailed content of Operations related to PDO data access abstraction layer in PHP. For more information, please follow other related articles on the PHP Chinese website!