pdo和mysql connect_PHP PDO与普通的mysql_connect

bd96500e110b49cbb3cd949968f18be7.png

Should I use php PDO or normal mysql_connect to execute database queries in PHP?

Which one is faster?

One of the big benefits of PDO is that the interface is consistent across multiple databases.

There are some cool functions for prepared statements too, which take some of the hassle out of escaping all your query strings. The portability of PDO is greater than mysql_connect.

So, should I use PDO for those reasons or stick to the traditional mysql_connect?

解决方案

PDO is a bit slower than the mysql_*

But it has great portability. PDO provides single interface across multiple databases. That means you can use multiple DB without using mysql_query for mysql, mssql_query for MS sql etc. Just use something like $db->query("INSERT INTO...") always. No matter what DB driver you are using.

So, for larger or portable project PDO is preferable. Even zend framework use PDO.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP连接MySQL数据库的三种方式分别是PDOMySQLi和mysqlPDOPHP Data Objects)是PHP提供的一种数据库操作扩展,支持多种数据库,其中包括MySQLPDO提供了一组统一的方法,使开发者能够以同样的方式对待不同的数据库。其优点包括安全性高、可移植性好、支持事务处理等。使用PDO连接MySQL数据库时,需要使用PDO的构造函数new PDO(),并传入相关的连接信息。 MySQLi(MySQL Improved)是PHP中的一种连接MySQL数据库的扩展,是对原有mysql扩展的增强和改进。MySQLi提供了面向对象和面向过程两种API,支持事务处理、预处理语句、存储过程等功能,同时也提供了对MySQL的新特性的支持。使用MySQLi连接MySQL数据库时,首先需要创建一个MySQLi对象,然后调用相应的方法进行连接、查询等操作。 mysql扩展是PHP早期版本提供的一种连接MySQL数据库的方式,但自PHP5.5.0版本开始已被弃用,不再被推荐使用。mysql扩展的连接方式相对简单,使用mysql_connect()函数进行连接,并传入相关的连接参数。然而,mysql扩展存在一些安全性和兼容性的问题,容易受到SQL注入等攻击。 综上所述,PDOMySQLi和mysqlPHP常用的三种连接MySQL数据库的方式。其中PDO具有较高的安全性和可移植性,使用方式统一;MySQLi支持较多的功能特性,包括事务处理、预处理语句等;mysql扩展已被弃用,不推荐使用。开发者可以根据自己的需求和情况选择合适的连接方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值