PHP中的PDO

Jun 20, 2016 pm 12:46 PM

PDO常用方法:

1、PDO::query()主要用于有记录结果返回的操作(PDOStatement),特别是select操作。

2、PDO::exec()主要是针对没有结果集合返回的操作。如insert,update等操作。返回影响行数。

3、PDO::lastInsertId()返回上次插入操作最后一条ID,但要注意:如果用insert into tb(col1,col2) values(v1,v2),(v11,v22)..的方式一次插入多条记录,lastinsertid()返回的只是第一条(v1,v2)插入时的ID,而不是最后一条记录插入的记录ID。

4、PDOStatement::fetch()是用来获取一条记录。配合while来遍历。

5、PDOStatement::fetchAll()是获取所有记录集到一个中。

6、PDOStatement::fetchcolumn([int column_indexnum])用于直接访问列,参数column_indexnum是该列在行中的从0开始索引值,但是,这个方法一次只能取得同一行的一列,只要执行一次,就跳到下一行。因此,用于直接访问某一列时较好用,但要遍历多列就用不上。

7、PDOStatement::rowcount()适用于当用query(“select …”)方法时,获取记录的条数。也可以用于预处理中。$stmt->rowcount();

8、PDOStatement::columncount()适用于当用query(“select …”)方法时,获取记录的列数。


注解:

1、选fetch还是fetchall?

用fetchall效率高,减少从数据库检索次数,但对于大结果集,用fetchall则给系统带来很大负担。数据库要向WEB前端传输量太大反而效率低。

2、fetch()或fetchall()有几个参数:

mixed pdostatement::fetch([int fetch_style [,int cursor_orientation [,int cursor_offset]]])

array pdostatement::fetchAll(int fetch_style)


fetch_style参数:

1、$row=$rs->fetchAll(PDO::FETCH_BOTH); FETCH_BOTH是默认的,可省,返回关联和索引。

2、$row=$rs->fetchAll(PDO::FETCH_ASSOC); FETCH_ASSOC参数决定返回的只有关联数组。

3、$row=$rs->fetchAll(PDO::FETCH_NUM); 返回索引数组

4、$row=$rs->fetchAll(PDO::FETCH_OBJ); 如果fetch()则返回对象,如果是fetchall(),返回由对象组成的二维数组

/** * 数据库配置 */$dsn = 'mysql:dbname=test;host=127.0.0.1;port=3306';$username = 'root';$password = '123456';/** * 连接数据库 */try{    $db = new PDO($dsn, $username, $password);}catch(PDOException $e){    die('fail');}$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);$db->exec("SET CHARACTER SET utf8");/** * 查询 */$name = 'li';$sql = sprintf("SELECT * FROM %s WHERE name LIKE %s", 'user', $name);$stmt = $db->prepare($sql);$stmt->execute();while($row = $stmt->fetch(PDO::FETCH_ASSOC)){    var_dump($row);}var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));/** * 添加 */$name = 'lilei'; $gender = 0;$sql = sprintf("INSERT INTO %s (name, gender) VALUES (%s, %d)", 'user', $name, $gender);$stmt = $db->prepare($sql);$stmt->execute();echo $db->lastInsertId();/** * 修改 */$id = 1; $name = 'hanmeimei'; $gender = 1;$sql = sprintf("UPDATE %s SET name = %s, gender = %d WHERE id = %d", 'user', $name, $gender, $id);$stmt = $db->prepare($sql);$stmt->execute();echo $stmt->rowCount();/** * 删除 */$id = 1;$sql = sprintf("DELETE FROM %s WHERE id = %d", 'user', $id);$stmt = $db->prepare($sql);$stmt->execute();echo $stmt->rowCount();/** * 断开连接 */$dbh=null;exit;
Copy after login


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles