php高版本不再使用mysql_connect()来连接数据库

 

想用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 起已废弃,并在将来会被移除。应使用 MySQLiPDO_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这个库来操作数据库。更加推荐。

posted @ 2015-09-20 11:24  王滔  阅读(43852)  评论(0编辑  收藏  举报