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.
转载于:https://www.cnblogs.com/Alight/p/5082812.html
php分享十五:php的数据库操作相关推荐
- 十五、Mysql数据库日志
Mysql数据库日志 十五.Mysql数据库日志 一.日志分类 1.错误日志 错误日志说明 错误日志的配置 错误日志轮询 安装数据库故障排除 2.普通查询日志 普通查询日志说明 普通查询日志的配置 3 ...
- LiveVideoStack线上交流分享 (十五) —— 熟悉技术的边界,实现1+1+13
熟悉技术应用场景,解决什么问题,方可知道采取什么技术,能够高概率解决业务问题,那么就需要为一项或多项技术点寻找到其发挥最大价值的前提条件,这样才能实现多个处理技术的组合,使业务问题能最大程度收益,实现 ...
- LiveVideoStackCon讲师热身分享 ( 十五 ) —— 教育场景下的实时音视频解决方案
LiveVideoStackCon 2018音视频技术大会是每年的多媒体技术人的盛宴,为了让参会者与大会讲师更多互动交流,我们推出了LiveVideoStackCon讲师热身分享第一季,在每周四晚19 ...
- MySQL高级:(十五)其他数据库日志
笔记来源:MySQL数据库教程天花板,mysql安装到mysql高级,强!硬! 文章目录 15.1 前言 15.2 MySQL日志 15.2.1 MySQL支持的日志类型 15.2.2 日志的弊端 1 ...
- 十六、Android数据库操作
16.1文件存储 16.1.1内部存储 openFileInput(FileName); openFileOutput(FileName,mode) //mode: //MODE_PRIVATE:代表 ...
- Android开发笔记(八十五)手机数据库Realm
Realm应用背景 Android自带的SQLite数据库,在多数场合能够满足我们的需求,但随着app广泛使用,SQLite也暴露了几个不足之处: 1.开发者编码比较麻烦,而且还要求开发者具备SQL语 ...
- 边记边学PHP-(十五)MySQL数据库基础操作2
四.使用可视化工具创建数据库 尽管使用命令行感觉更像我们程序猿,可是我还是比較喜欢使用workbench来创建数据库. 首先打开workbench , 一个比較友好的界面就打开了,哈哈.我还是比較喜欢 ...
- Java云同桌学习系列(十五)——MySQL数据库
本博客java云同桌学习系列,旨在记录本人学习java的过程,并与大家分享,对于想学习java的同学,我希望这个系列能够鼓励大家一同与我学习java,成为"云同桌". 每月预计保持 ...
- 无字天书之Python第十五页(Excel表格操作)
博客内容 传送门 正文读 正文写 到此Python基础就此结束- 传送门 无字天书之Python第一页 无字天书之Python第二页 无字天书之Python第三页 无字天书之Python第四页 无字天 ...
最新文章
- ALS爱立思脚本调用参考
- Golang入门教程(二)Ubuntu16.04下安装golang(实例:Golang 定时任务管理器)
- 【学生选课系统经典】C#与SQLSERVER连接:ASP.NET网站(服务器端,IIS发布)
- CUDA程序kernel函数的运行时间限制
- 鼠标跟随事件jQuery
- 数据库设计软件-powerdesign,导出表结构为Word文档
- jdbc和mysql做游戏排行榜_MySQL 和 JDBC编程
- tensorflow keras 构建神经网络、Alex net、VGG、CNN网络
- 看懂Oracle的执行计划
- 浏览器主页被360篡改怎么办?
- Python自省机制
- 缓解眼睛疲劳,护眼调节色温软件推荐
- SpringCloudAlibaba之Nacos
- vmd python 命令_VMD常用命令
- TransE全文中文翻译(Translating Embeddings for Modeling Multi-relational Data)
- Linux中curl命令详解
- pascal语法基础
- 行业资讯 | 做到这几点,Revit建模效率提升,实用.
- Arduino串口控制DY-SV5W音频播放
- 去信任外包虚荣地址生成