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分享十五:php的数据库操作的更多相关文章
- 第十五章 MySQL 数据库
学习要点:1.Web 数据库概述2.MySQL 的操作3.MySQL 常用函数4.SQL 语句详解5.phpMyadmin 一.Web数据库概述 现在,我们已经熟悉了PHP 的基础知识,这是我们想暂时 ...
- 十分钟学会mysql数据库操作
Part1:写在最前 MySQL安装的方式有三种: ①rpm包安装 ②二进制包安装 ③源码安装 这里我们推荐二进制包安装,无论从安装速度还是用于生产库安装环境来说,都是没问题的.现在生产库一般采用My ...
- 雷林鹏分享:CodeIgniter常用的数据库操作类
在 CodeIgniter 中,使用数据库是非常频繁的事情.你可以使用框架自带的数据库类,就能便捷地进行数据库操作. 初始化数据库类 依据你的数据库配置载入并初始化数据库类: $this->lo ...
- 边记边学PHP-(十五)MySQL数据库基础操作2
四.使用可视化工具创建数据库 尽管使用命令行感觉更像我们程序猿,可是我还是比較喜欢使用workbench来创建数据库. 首先打开workbench , 一个比較友好的界面就打开了,哈哈.我还是比較喜欢 ...
- php分享十五:php的命令行操作
一:像命令行传参数方法: 1: 使用$argc $argv 用法: /usr/local/php/bin/php ./getopt.php 123 456 2:使用getopt函数() http:/ ...
- Oracle得知(十五):分布式数据库
--分布式数据库的独立性:分布数据的独立性指用户不必关心数据怎样切割和存储,仅仅需关心他须要什么数据. --本地操作 SQL> sqlplus scott/tiger --远程操作 SQL> ...
- Linux 笔记 - 第十五章 MySQL 常用操作和 phpMyAdmin
博客地址:http://www.moonxy.com 一.前言 前面几章介绍了 MySQL 的安装和简单的配置,只会这些还不够,作为 Linux 系统管理员,我们还需要掌握一些基本的操作,以满足日常管 ...
- Python之路【第二十五篇】:数据库之pymysql模块
数据库进阶 一.pymysql模块 pymysql是Python中操作Mysql的模块,其使用的方法和py2的MySQLdb几乎相同. 二.pymysql模块安装 pip install pymysq ...
- uni-app开发经验分享十五: uni-app 蓝牙打印功能
最近在做uni-app项目时,遇到了需要蓝牙打印文件的功能需要制作,在网上找到了一个教程,这里分享给大家. 引入tsc.js 简单得引入到自己所需要得页面中去,本次我们只要到了标签模式,他同时还有账单 ...
随机推荐
- 理解CPU steal time
http://melody-dc.com/2015/11/21/%E7%90%86%E8%A7%A3CPU-steal-time/ http://www.cnblogs.com/yjf512/p/33 ...
- MySQL 内存和CPU优化相关的参数
mysql> SHOW GLOBAL STATUS LIKE 'innodb%read%'; +---------------------------------------+--------- ...
- golang之log rotate
操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 golang的log模块提供的有写日志功能,示例代码如下: /* golang log example E-Mail : ...
- eclipse debug模式下总是自动跳到ThreadPoolExecutor.java类
1.情景展示 使用eclipse,debug模式运行项目时,总是会调用这个ThreadPoolExecutor.java类,明明没有打断点却自动停止运行. 2.原因分析 在eclipse中,默认是 ...
- django之异常错误
现象:最近需要抓取一些网页的信息,但发现Python的乱码问题相对Java来说,不太一样.按照以往Java解决中文乱码问题的思路去解决Python乱码,貌似行不通,报错信息: SyntaxError: ...
- python str方法之ljust、rjust、center
# -*- coding: cp936 -*- #python 27 #xiaodeng #str方法之ljust.rjust.center #http://www.runoob.com/python ...
- alias别名使用
rhel系列的别名使用,方便操作! 功能说明:设置指令的别名.语 法:alias [别名] = [指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举 例 :ermao@lo ...
- IntelliJ IDEA遇到Unable to parse template “Class”错误
在新安装的Ubuntu16下运行IntelliJ IDEA时, 遇到一个错误,在新建class的时候,提示Unable to parse template “Class” 通过查看 Settings ...
- ios判断是否有中文
//判断是否有中文 -(BOOL)IsChinese:(NSString *)str { ; i< [str length];i++){ int a = [str characterAtInde ...
- 10行代码解析krc歌词文件
互联网上,我们常见的歌词格式有 LRC.TRC(天天动听歌词).KRC(KuGou ResourCe,酷狗资源文件)和 QRC(QQ音乐歌词):在影视制作中,人们通常会用其他的卡拉 OK 字幕格式,例 ...