php分享十五:php的数据库操作
一:术语解释:
What is an Extension?
API和扩展不能理解为一个东西,因为扩展不一定暴露一个api给用户
The PDO MySQL driver extension, for example, does not expose an API to the PHP programmer, but provides an interface to the PDO layer above it.
The terms API and extension should not be taken to mean the same thing, as an extension may not necessarily expose an API to the programmer.
参考:http://php.net/manual/zh/mysqlinfo.terminology.php
二:扩展选择:
ext/mysqli | PDO_MySQL | ext/mysql | |
---|---|---|---|
PHP version introduced | 5.0 | 5.1 | 2.0 |
Included with PHP 5.x | Yes | Yes | Yes |
Included with PHP 7.x | Yes | Yes | No |
Development status | Active | Active | Maintenance only in 5.x; removed in 7.x |
Lifecycle | Active | Active | Deprecated in 5.x; removed in 7.x |
Recommended for new projects | Yes | Yes | No |
OOP Interface | Yes | Yes | No |
Procedural Interface | Yes | No | Yes |
API supports non-blocking, asynchronous queries with mysqlnd | Yes | No | No |
Persistent Connections | Yes | Yes | Yes |
API supports Charsets | Yes | Yes | Yes |
API supports server-side Prepared Statements | Yes | Yes | No |
API supports client-side Prepared Statements | No | Yes | No |
API supports Stored Procedures | Yes | Yes | No |
API supports Multiple Statements | Yes | Most | No |
API supports Transactions | Yes | Yes | No |
Transactions can be controlled with SQL | Yes | Yes | Yes |
Supports all MySQL 5.1+ functionality | Yes | Most | No |
三:选择mysql驱动(Choosing a library)
mysqli,mysql,pdo底层都是用c写的,有两种底层c库可以使用(mysqlnd library 和 libmysqlclient library),通过编译参数可以更改区别:
mysqlnd更胜一筹;
从php5.3开始,mysqlnd成为php的一部分发布,mysqlnd提供的高级功能有:lazy connections and query caching (这两个功能libmysqlclient library是没有的)
编译配置:
// Recommended, compiles with mysqlnd $ ./configure --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd // Alternatively recommended, compiles with mysqlnd as of PHP 5.4 $ ./configure --with-mysqli --with-pdo-mysql --with-mysql // Not recommended, compiles with libmysqlclient $ ./configure --with-mysqli=/path/to/mysql_config --with-pdo-mysql=/path/to/mysql_config --with-mysql=/path/to/mysql_config
注意:可以分别指定mysqli, mysql, pdo 扩展的底层c库
MySQL native driver (mysqlnd) | MySQL client server library (libmysqlclient) | |
---|---|---|
Part of the PHP distribution | Yes | No |
PHP version introduced | 5.3.0 | N/A |
License | PHP License 3.01 | Dual-License |
Development status | Active | Active |
Lifecycle | No end announced | No end announced |
PHP 5.4 and above; compile default (for all MySQL extensions) | Yes | No |
PHP 5.3; compile default (for all MySQL extensions) | No | Yes |
Compression protocol support | Yes (5.3.1+) | Yes |
SSL support | Yes (5.3.3+) | Yes |
Named pipe support | Yes (5.3.4+) | Yes |
Non-blocking, asynchronous queries | Yes | No |
Performance statistics | Yes | No |
LOAD LOCAL INFILE respects the open_basedir directive | Yes | No |
Uses PHP's native memory management system (e.g., follows PHP memory limits) | Yes | No |
Return numeric column as double (COM_QUERY) | Yes | No |
Return numeric column as string (COM_QUERY) | Yes | Yes |
Plugin API | Yes | Limited |
Read/Write splitting for MySQL Replication | Yes, with plugin | No |
Load Balancing | Yes, with plugin | No |
Fail over | Yes, with plugin | No |
Lazy connections | Yes, with plugin | No |
Query caching | Yes, with plugin | No |
Transparent query manipulations (E.g., auto-EXPLAIN or monitoring) | Yes, with plugin | No |
MySQL native driver (mysqlnd) | MySQL client server library (libmysqlclient) | |
---|---|---|
Part of the PHP distribution | Yes | No |
PHP version introduced | 5.3.0 | N/A |
License | PHP License 3.01 | Dual-License |
Development status | Active | Active |
Lifecycle | No end announced | No end announced |
PHP 5.4 and above; compile default (for all MySQL extensions) | Yes | No |
PHP 5.3; compile default (for all MySQL extensions) | No | Yes |
Compression protocol support | Yes (5.3.1+) | Yes |
SSL support | Yes (5.3.3+) | Yes |
Named pipe support | Yes (5.3.4+) | Yes |
Non-blocking, asynchronous queries | Yes | No |
Performance statistics | Yes | No |
LOAD LOCAL INFILE respects the open_basedir directive | Yes | No |
Uses PHP's native memory management system (e.g., follows PHP memory limits) | Yes | No |
Return numeric column as double (COM_QUERY) | Yes | No |
Return numeric column as string (COM_QUERY) | Yes | Yes |
Plugin API | Yes | Limited |
Read/Write splitting for MySQL Replication | Yes, with plugin | No |
Load Balancing | Yes, with plugin | No |
Fail over | Yes, with plugin | No |
Lazy connections | Yes, with plugin | No |
Query caching | Yes, with plugin | No |
Transparent query manipulations (E.g., auto-EXPLAIN or monitoring) |
四:mysql字符集设置
字符设置必须用特定的函数来设置,而不能用query来处理
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// Will NOT affect $mysqli->real_escape_string();
$mysqli->query("SET NAMES utf8");
// Will NOT affect $mysqli->real_escape_string();
$mysqli->query("SET CHARACTER SET utf8");
// But, this will affect $mysqli->real_escape_string();
$mysqli->set_charset('utf8');
// But, this will NOT affect it (utf-8 vs utf8) -- don't use dashes here
$mysqli->set_charset('utf-8');
?>
注意:
设置utf8编码时,不能用utf-8,而是utf8(因为在mysql中设置编码不能包含-)
Note: Possible UTF-8 confusion
Because character set names in MySQL do not contain dashes, the string "utf8" is valid in MySQL to set the character set to UTF-8. The string "utf-8" is not valid, as using "utf-8" will fail to change the character set.
相关文章
- PHP数据库操作:使用ORM
- PHP Socket编程 之 php实现http服务
- 关于PHP+jQuery-ui拖动浮动层排序并保存到数据库实例
- dns未设置 PHP Warning: file_get_contents():php_network_getaddresses: getaddrinfo failed:
- PHP数据库操作:使用ORM
- PHP fastcgi模式大文件上传500错误
- [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...
- PHP采集库-Snoopy.class.php
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)--续
- php:在linux上用sudo提升权限(centos 8 / PHP 7.4.2)
- php实现计划任务与持续进程实例 fsockopen
- PHP连接数据库学习手册
- Linux下安装php环境并且配置Nginx支持php-fpm模块
- php saas 架构设计,SaaS的几种架构解析
- PHP(面向对象)连接数据库,实现基本的增删改查
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)--续
- thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)
- [PHP]Yaf + composer 引起大幅性能下降
- [php-src]一个Php扩展的结构
- Atitit php pdo的api使用 目录 1.1. PHP PDO简介1 1.2. 若要使用数据库长连接,:PDO::ATTR_PERSISTENT1 2. 其他设置2 2.1. )、P
- atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js
- atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js
- PHP MySQL 创建数据库
- php数据库的增删改查
- php对bom的处理
- PHP实现智能语音播报