想用php生成一個mysql數據字典導出來,用到下面代碼會php
$mysql_conn = mysql_connect ( "$dbserver", "$dbusername", "$dbpassword" ) or die ( "Mysql connect is error." );mysql
在php5.5.12版本運行會提示web
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in D:\soft\develop\php\wamp\2.5\wamp\www\generate_mysql.php on line 16sql
看來會廢棄了,不建議使用了,程序沒法運行的。使用mysqli or PDO 來替代。到高版本,根本沒法使用這個函數了。shell
我想知道哪一個php版本開始就會開始不建議使用這個函數了,因此去官網www.php.net搜索這個函數。有這樣的介紹:數據庫
本擴展自 PHP 5.5.0 起已廢棄,並在未來會被移除。應使用 MySQLi 或 PDO_MySQL 擴展來替換之。參見 MySQL:選擇 API 指南以及相關 FAQ 以獲取更多信息。用以替代本函數的有:api
地址:http://php.net/manual/zh/function.mysql-connect.php安全
MySQL Native Driver is a replacement for the MySQL Client Library (libmysqlclient). MySQL Native Driver is part of the official PHP sources as of PHP 5.3.0.ide
The MySQL database extensions MySQL extension, mysqli and PDO MYSQL all communicate with the MySQL server. In the past, this was done by the extension using the services provided by the MySQL Client Library. The extensions were compiled against the MySQL Client Library in order to use its client-server protocol.函數
With MySQL Native Driver there is now an alternative, as the MySQL database extensions can be compiled to use MySQL Native Driver instead of the MySQL Client Library.
MySQL Native Driver is written in C as a PHP extension.
php5.3版本開始,使用的mysql擴展是集成在php源碼中發佈。
1.版權考慮:原來與mysql進行通訊的庫(mysql Client Library),是由mysqlAB公司(如今是甲骨文)所寫,那麼就是在該公司的協議下發布(版權)。那麼有些功能就會被禁用掉。
mysqlnd這個驅動,是由zend 公司開發的MySQL數據庫驅動,採用PHP開源協議(即 PHP license)避免了任何可能存在的版權問題。而舊的libmysql是有Mysql AB公司(如今的Oracle Corporation)開發,依照mysql license。
還記得咱們在編譯php的時候,若是須要php連接mysql數據庫,那麼必須編譯的時候指定一個項:
--with-mysql=/usr/local/mysql 這裏是指定mysql客戶端庫的位置。
後面就是mysql的安裝目錄。由於與mysql進行通訊,須要按照mysql的協議來進行通訊,而mysql官方是發佈了客戶端((libmysqlclient庫),因此這裏就是指定去mysql的安裝目錄下搜索客戶端庫的位置。
這樣的確麻煩。
如今寫入php源碼一部分,會解決過去的版本發佈的問題。至關於安裝了php源碼,就安裝了與mysql進行通訊的庫。
是這樣編譯了
./configure --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \
上面的結果是,mysql、mysqli、pdo這三個操做mysql的擴展,都配置使用mysqlnd庫來操做mysql
--with-mysql項指定使用mysqlnd客戶端庫了,根本不須要依賴於mysql的安裝路徑了。由於是使用php源碼中本身的的庫。
這個庫是一個c語言編寫的,以擴展形式加入php引擎。
這個庫就在php的源碼包中,本身編譯安裝,就生成在本身在php中,根本不須要依賴於神馬mysql提供的客戶端了
因此顧名思義,叫作Native driver,本地驅動(操做mysql的驅動)。
2.內存使用效率提升。之前是複製數據兩份,如今只需一份。
新版本因爲擴展集成在php源碼中,是php一部分。mysql_fetch_assoc()是複製一份數據到php中了。之前就是複製一份到擴展中,同時複製一份到php中。因此是兩份。
總結一句話就是:操做mysql有三個可供選擇的擴展,mysql、mysqli、pdo。而mysqli and PDO MYSQL做爲推薦的擴展。而不是mysql原來的擴展
給咱們作接口服務的啓發
到高版本,沒有兼容舊太舊版本的函數,爲何這樣子?從php官方組織維護源碼角度來講,這個函數確定是沒啥優點了,去進行優化這個函數,還不如對它進行廢棄掉
咱們在網站,須要提供給接口給公司內部其餘子系統調用。也會存在接口升級,原來的接口設計有缺陷,不想去修復了。乾脆建議使用新版本的接口了。
思考,我何不也弄一個相似的提高呢。提示調用方升級到新接口去使用。
忽然發現,從溝通成本角度考慮,把提示信息放在接口返回給調用方,會推動改進的速度。
爲何官方要把這個函數禁用掉?而不是去修復,優化呢?
性能考慮,安全考慮。去官網找答案
Recommended API
It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development, as it has been deprecated as of PHP 5.5.0 and will be removed in the future. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same(三個擴展的性能不相上下). Although the performance of the extension contributes only a fraction of the total run time of a PHP web request(性能考慮在php的web請求中是一小部分考慮,即不單單是性能考慮). Often, the impact is as low as 0.1%(影響是很小的,0.1%)
看官網介紹,不是性能考慮才棄用那個擴展。反正就是棄用,不想去維護那個了。我也沒搞清楚。不過深有同感,舊的擴展代碼量也很多,去優化,接口結構始終沒法達到質的提高,還不如新開一個?
mysqli擴展使用了mysqlnd這個庫來操做數據庫。更加推薦。