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 简单得引入到自己所需要得页面中去,本次我们只要到了标签模式,他同时还有账单 ...
随机推荐
- Aerospike系列:8:集群宕机演练
1:初始的集群状态 2:关掉192.168.91.133:3000 3:再关掉192.168.91.135:3000 3:再关掉192.168.91.144:3000 5:恢复192.168.91.1 ...
- js 实现继承的6种方式(逐渐优化)
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 使用 git post-receive 钩子部署服务端代码
在 git 中提交服务器源码的时候,如果能够直接更新到测试服务器,并且重启服务使其生效,会节省懒惰的程序员们大量的时间. git 的 Server-side hook (服务端钩子/挂钩)可以用来做件 ...
- 〖Linux〗bash和expect执行ssh命令行sshcmd.exp
#!/usr/bin/expect -f # sudo apt-get install expect # ./ssh.exp user passwd server set user [lrange $ ...
- QTP Test ,VAPI-XP Test,LR Test 和ALM 集成远程分布式执行遇到的“access is denied ” “unspecified error”问题
大家都知道QTP与ALM (QC的升级版)集成是最好的一个分布式执行的结合.因为毕竟QTP是一个商业软件,HP当然不会让你去跟其他的open source的工具去集成,要不他到哪里去挣钱. 有时候服务 ...
- 读取csv文件并打印其结果
In [5]: import pandas as pd In [6]: df=pd.read_csv('https://raw.githubusercontent.com/alstat/Analysi ...
- java for语句
//for语句 public class Test16{ public static void main(String args[]){ for (int i=0;i<10;i+=1){ if ...
- 在xpage上怎么用jdbc去连接sql server呀
你去http://www.openntf.org/Internal/home.nsf 下載以下對應版本最新控件 XPages Extension Library 這裏面已經包括OSGI功能 OSGI在 ...
- java的配置方式简介
1,java的配置方式简介java的配置方式是为了代替使用xml配置方式,主要使用两个注解:@Configuration//通过该注解来表明该类是一个spring的配置,相当于一个xml文件@Comp ...
- 通过ribbon 根据服务名获取所有服务实例的IP和端口列表
代码使用SpringCloud版本E3 业务场景: 今天遇到一个业务场景,要求根据服务名获取当前微服务集群中所有的对应服务实例的IP和端口,通过分析源码推算出了写法. 原理简述: 如果代码中引入了sp ...