php中PDO入门学习笔记
PDO是高版本php中的一个mysql数据库操作功能了,我们下面就一起来看看关于php中PDO的一些学习笔记,有兴趣了解PDO的朋友可一起来看看哦.
一、PDO简介
PDO是PHP Data Object的简称,用于定义数据库访问的抽象层,统一各种数据库的访问接口。PDO有如下特性:
编码一致性:PDO支持多种数据库扩展,并为其提供统一的访问接口
灵活性:PDO提供了统一的SQL操作方法和属性
高效性:PDO是用C编写、PHP编译的
面向对象:PDO完全采纳面向对象的思想
PDO支持的数据库:
pdo支持的数据库
但是,需要注意的是,PDO只是提供了一组数据库访问接口,利用PDO本身不能操作任何数据库。
二、PDO对象
利用PDO的构造函数连接特定的数据库,创建一个PDO对象。在利用PDO之前,需要开启PDO扩展。以PHP+MySQL为例,
开启PHP对PDO的扩展:在php.ini中去掉extension=php_pdo.dll的注释。
开启MySQL对PDO的扩展:在my.ini中去掉extension=php_pdo_mysql.dll的注释。
利用phpinfo()可以查看是否开启PDO扩展。
1、连接数据库
PDO连接数据库有三种方式:
1.1 通过参数形式连接(推荐)
$dsn = "mysql:host=localhost;dbname=xxx";
$username = "xxxxx";
$pwd = "xxxx";
$pdo = new PDO($dsn,$username,$pwd);
PDO构造函数还有一个$options参数,它是一个数组,用于配置运行中的数据库,如是否开启自动提交、设置结果集的返回方式等。
1.2 通过uri连接
首先建立一个文件,保存数据源,文件内容形如:mysql:host=localhost;dbname=xxx。然后在程序中引入该文件。
$dsn = "uri:file://保存数据源配置的文件路径";
$username = "xxxxx";
$pwd = "xxxx";
$pdo = new PDO($dsn,$username,$pwd);
1.3 通过配置文件连接
在php.ini中的任意一个位置添加数据源的配置:
pdo.dsn.test=”mysql:host=localhost;dbname=xxxx”;
test是自定义的数据源名称。为方便,可以在php.ini的首行中添加,然后重启服务器,在程序中引入该数据源
$dsn = "test"; //数据源名称
$username = "xxxxx";
$pwd = "xxxx";
$pdo = new PDO($dsn,$username,$pwd);
2、PDO对象的常见方法和属性
方法或属性 描述
exec() 执行一条SQL语句,返回受影响的行数。用于增删改,对查询返回0
query() 执行一条SQL语句,返回PDOStatement对象,用于保存结果集
prepare() 执行一条SQL语句,返回PDOStatement对象
quote() 返回一个添加引号的字符串,用于SQL语句,可防止SQL注入
lastInsertId 返回最后插入的ID
setAttribute() 设置数据库连接属性
getAttribute() 获取数据库连接属性
errorCode() 获取跟数据库句柄上一次操作相关的SQLSTATE
errorInfo() 获取跟数据库句柄上一次操作相关的错误信息
beginTransaction() 开启一个事务。需要先关闭自动提交
commit() 提交事务
rollBack() 回滚事务
inTransaction() 检查操作是否在事务内
三、PDOStatement对象
PDO对象的query()和prepare()方法均会返回一个PDOStatement对象,该对象可以用于PDO的预处理执行。其常见方法如下:
方法 描述
execute() 执行一条预处理语句
rowCount() 返回上一个SQL语句影响的行数
fetch() 从结果集中获取一行
fetchAll() 返回一个包含结果中所有行的数组
setFetchMode() 设置默认获取结果集的模式
fetchObject() 获取结果集下一行并作为对象返回
fetchColumn() 获取结果集下一行的单独一列
bindParam() 绑定一个参数到指定的变量名
bindValue() 将某个值绑定到指定的参数
bindColumn() 绑定一列到PHP变量
getColumnMeta() 返回结果集中一列的元素据
columnCount() 返回结果中的列数
setAttribute() 设置一个语句属性
getAttribute() 获取一个语句属性
errorCode() 获取跟数据库句柄上一次操作相关的SQLSTATE
errorInfo() 获取跟数据库句柄上一次操作相关的错误信息
debugDumpparams() 打印一条SQL预处理语句
nextRowset() 在一个多行集语句句柄中推进到下一个行集
四、异常模式
PDO支持三种异常模式:
默认模式:PDO::ERRMODE_CLIENT
警告模式:PDO::ERRMODE_WARNING
异常模式:PDO::ERRMODE_EXCEPTION(推荐)
可以在PDO的构造函数的第四个$options参数或用PDO对象的setAttribute()方法设置。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
