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
?
二:擴展選擇:
?
| 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庫
| 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 |
?
| 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的数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 肝S3&nbsp;见异常信号灶(
- 下一篇: 5、运算符