想用php生成一個mysql數據字典導出來,用到下面代碼會
$mysql_conn = mysql_connect ( "$dbserver", "$dbusername", "$dbpassword" ) or die ( "Mysql connect is error." );
在php5.5.12版本運行會提示
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 16
看來會廢棄了,不建議使用了,程序無法運行的。使用mysqli or PDO 來替代。到高版本,根本無法使用這個函數了。
我想知道哪個php版本開始就會開始不建議使用這個函數了,所以去官網www.php.net搜索這個函數。有這樣的介紹:
本擴展自 PHP 5.5.0 起已廢棄,並在將來會被移除。應使用 MySQLi 或 PDO_MySQL 擴展來替換之。參見 MySQL:選擇 API 指南以及相關 FAQ 以獲取更多信息。用以替代本函數的有:
地址: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.
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這個庫來操作數據庫。更加推薦。
