简体   繁体   English

切换到mysqli一个好主意?

[英]switch to mysqli a good idea?

I'm considering switching to mysqli for all my php projects. 我正在考虑为我所有的php项目切换到mysqli。 The way my code is written (I run very simple websites and built my own basic framework which I use across all of them) I shouldn't have too many problems modifying the functions and classes. 我的代码编写方式(我运行非常简单的网站并构建我自己的基本框架,我在其中使用所有这些)我不应该在修改函数和类时遇到太多问题。

However, i've only heard positive things about prepared statements, bar a few grumblings about the php functions available, most notably the lack of a simple replacement for using mysql_fetch_array in a while. 但是,我只听到关于准备好的语句的积极的事情,关于可用的php函数的一些抱怨,最值得注意的是缺少一段时间使用mysql_fetch_array的简单替代。

This sounds a bit too good to be true, so I wondered if anyone could highlight some of the issues with using prepared statements, such as speed and resource usage. 这听起来有点太好了,所以我想知道是否有人可以强调使用预准备语句的一些问题,例如速度和资源使用。

Programming for prepared statements takes a bit of getting used to if you're used to just appending variables to query strings. 如果您习惯于将变量附加到查询字符串,那么对预准备语句进行编程需要一些时间来习惯。 MySQL uses positional parameters (your query will contain question marks where the replace vars belong). MySQL使用位置参数(您的查询将包含替换变量所属的问号)。 The best bet is to put this into your existing database abstraction. 最好的办法是将它放入现有的数据库抽象中。 If that abstraction was written properly, you shouldn't be calling mysql_fetch_array outside the wrapper anyway. 如果正确编写了抽象,那么你不应该在包装器之外调用mysql_fetch_array。

The solution to this problem is just to collect all the rows in advance, but of course that assumes that you don't retrieve 1000 rows and just ask for the first one. 这个问题的解决方案只是提前收集所有行,但当然假设您没有检索1000行而只是要求第一行。 This is a change that you should make regardless of mysqli. 无论mysqli如何,这都是您应该做出的改变。

Finally, some statements are not easily replaced by parameters, such as queries using the in('x', 'y', 'z') syntax with variable numbers of arguments. 最后,一些语句不容易被参数替换,例如使用带有可变数量参数的in('x', 'y', 'z')语法的查询。 It can be done, but you'll probably want to enrich your database abstraction to allow it to create the queries as well as execute them. 它可以完成,但您可能希望丰富数据库抽象,以允许它创建查询以及执行它们。

The tradeoff, though, is definitely worth it, in terms of performance and safety. 然而,在性能和安全性方面,这种权衡无疑是值得的。 The additional processing on the PHP side is usually outweighed by the cached execution plans for queries on the MySQL side, and you are immune to many of the most common SQL injection vulnerabilities. PHP端的额外处理通常被MySQL端查询的缓存执行计划所抵消,并且您不受许多最常见的SQL注入漏洞的影响。

Hope that helps, Joe 希望有所帮助,乔

Prepared statements are so good that once you get used to them it's painful to use escaping functions again. 准备好的语句非常好,一旦你习惯了它们,再次使用转义函数是很痛苦的 Period. 期。

However, all DB libraries I've ever used (including oci8 and sqlsrv...) introduce one quirk or another. 但是,我曾经使用过的所有DB库(包括oci8和sqlsrv ......)都引入了一个奇怪或者另一个。 So I basically encapsulate whatever library I use with a simple set of custom classes that provide the features in the way I like: 所以我基本上使用一组简单的自定义类来封装我使用的库,这些自定义类以喜欢的方式提供功能:

  • Name based parameters: WHERE foo = :foo 基于名称的参数: WHERE foo = :foo
  • Parameter passed by value in associative array (rather than binding to individual PHP variables): $params = array('foo' => 33) 在关联数组中通过值传递的参数(而不是绑定到单个PHP变量): $params = array('foo' => 33)
  • One line execution: $res = $Db->query($sql, $params); 一行执行: $res = $Db->query($sql, $params);
  • Resultsets are objects that implement the Iterator interface, so I can loop with foreach($res as $row) Resultsets是实现Iterator接口的对象,因此我可以使用foreach($res as $row)循环foreach($res as $row)

Adopting such policy makes the exact syntax or function set less important. 采用这样的策略使得确切的语法或功能集不那么重要。

Whatever, while this can be accomplished with almost any library, it helps if it provides native param binding so (for instance) you don't have to guess the data type. 无论如何,虽然这可以通过几乎任何库来实现,但如果它提供本机参数绑定(例如),则无需猜测数据类型。 Also, some advanced functionality like transactions can't simply be done with plain mysql functions. 此外,某些高级功能(如事务)不能简单地使用普通的mysql函数完成。

PDO could have been a nice alternative but most of its drivers are basically abandoned so you actually loose the benefit of having a DB-agnostic abstraction layer while you enjoy it pitfalls. PDO可能是一个不错的选择,但它的大多数驱动程序基本上都被放弃了,所以当你享受陷阱时,你实际上已经失去了拥有DB不可知抽象层的好处。

IMHO, the mere fact that you are asking suggest that you should give mysqli a chance. 恕我直言,你要问的事实表明你应该给mysqli一个机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM