Operations related to PDO data access abstraction layer in PHP

一个新手
Release: 2023-03-15 22:10:01
Original
1220 people have browsed it

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,帐号,密码
Copy after login

//2.Write SQL statement

$sql ="select * from info";  
$sql ="insert into info values('004','王六','男','n007','1994-02-11')";
Copy after login

//3.Execute SQL statement

$stm = $pdo->query($sql); //查询语句用query,返回的是结果
$arr = $pdo->exec($sql);//增删改用exec,返回的是执行的行数
Copy after login

//4. Read data from the PDOStatement object

$arr =$stm->fetch(PDO::FETCH_NUM);//默认不选为PDO::FETCH_BOTH  fetch为选择一条数据
$arr = $stm->fetchAll(PDO::FETCH_BOTH);//fetchAll为全选
Copy after login

//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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!