日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php分享十五:php的数据库操作

發布時間:2025/5/22 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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/mysqliPDO_MySQLext/mysql
PHP version introduced5.05.12.0
Included with PHP 5.xYesYesYes
Included with PHP 7.xYesYesNo
Development statusActiveActiveMaintenance only in 5.x; removed in 7.x
LifecycleActiveActiveDeprecated in 5.x; removed in 7.x
Recommended for new projectsYesYesNo
OOP InterfaceYesYesNo
Procedural InterfaceYesNoYes
API supports non-blocking, asynchronous queries with mysqlndYesNoNo
Persistent ConnectionsYesYesYes
API supports CharsetsYesYesYes
API supports server-side Prepared StatementsYesYesNo
API supports client-side Prepared StatementsNoYesNo
API supports Stored ProceduresYesYesNo
API supports Multiple StatementsYesMostNo
API supports TransactionsYesYesNo
Transactions can be controlled with SQLYesYesYes
Supports all MySQL 5.1+ functionalityYesMostNo

?

三:選擇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 distributionYesNo
PHP version introduced5.3.0N/A
LicensePHP License 3.01Dual-License
Development statusActiveActive
LifecycleNo end announcedNo end announced
PHP 5.4 and above; compile default (for all MySQL extensions)YesNo
PHP 5.3; compile default (for all MySQL extensions)NoYes
Compression protocol supportYes (5.3.1+)Yes
SSL supportYes (5.3.3+)Yes
Named pipe supportYes (5.3.4+)Yes
Non-blocking, asynchronous queriesYesNo
Performance statisticsYesNo
LOAD LOCAL INFILE respects the?open_basedir directiveYesNo
Uses PHP's native memory management system (e.g., follows PHP memory limits)YesNo
Return numeric column as double (COM_QUERY)YesNo
Return numeric column as string (COM_QUERY)YesYes
Plugin APIYesLimited
Read/Write splitting for MySQL ReplicationYes, with pluginNo
Load BalancingYes, with pluginNo
Fail overYes, with pluginNo
Lazy connectionsYes, with pluginNo
Query cachingYes, with pluginNo
Transparent query manipulations (E.g., auto-EXPLAIN or monitoring)Yes, with pluginNo

?

?MySQL native driver (mysqlnd)MySQL client server library (libmysqlclient)
Part of the PHP distributionYesNo
PHP version introduced5.3.0N/A
LicensePHP License 3.01Dual-License
Development statusActiveActive
LifecycleNo end announcedNo end announced
PHP 5.4 and above; compile default (for all MySQL extensions)YesNo
PHP 5.3; compile default (for all MySQL extensions)NoYes
Compression protocol supportYes (5.3.1+)Yes
SSL supportYes (5.3.3+)Yes
Named pipe supportYes (5.3.4+)Yes
Non-blocking, asynchronous queriesYesNo
Performance statisticsYesNo
LOAD LOCAL INFILE respects the?open_basedir directiveYesNo
Uses PHP's native memory management system (e.g., follows PHP memory limits)YesNo
Return numeric column as double (COM_QUERY)YesNo
Return numeric column as string (COM_QUERY)YesYes
Plugin APIYesLimited
Read/Write splitting for MySQL ReplicationYes, with pluginNo
Load BalancingYes, with pluginNo
Fail overYes, with pluginNo
Lazy connectionsYes, with pluginNo
Query cachingYes, with pluginNo
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的数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。