切换到mysqli的一个好主意? [英] switch to mysqli a good idea?

查看:54
本文介绍了切换到mysqli的一个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为我所有的php项目切换到mysqli.代码的编写方式(我运行非常简单的网站并构建了自己的基本框架,供所有网站使用),在修改函数和类时应该不会有太多问题.

I'm considering switching to mysqli for all my php projects. 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.

但是,我只听说过有关预准备语句的正面信息,对可用的php函数不满,最明显的是一段时间内缺少使用mysql_fetch_array的简单替代方法.

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.

这听起来有些不可思议,所以我想知道是否有人可以突出显示使用准备好的语句时遇到的一些问题,例如速度和资源使用情况.

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.

推荐答案

准备好的语句是如此之好,以至于一旦习惯了它们,再次使用转义功能就很痛苦了.期间.

Prepared statements are so good that once you get used to them it's painful to use escaping functions again. Period.

但是,我曾经使用过的所有数据库库(包括oci8和sqlsrv ...)都会引入一个或另一个怪癖.因此,我基本上用一套简单的自定义类封装我使用的任何库,这些自定义类以 I 的方式提供功能,例如:

However, all DB libraries I've ever used (including oci8 and sqlsrv...) introduce one quirk or another. So I basically encapsulate whatever library I use with a simple set of custom classes that provide the features in the way I like:

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

采用这种策略会使确切的语法或功能集变得不那么重要.

Adopting such policy makes the exact syntax or function set less important.

无论如何,尽管几乎所有库都可以完成此操作,但是如果它提供了本机参数绑定,则将有所帮助,因此(例如)您不必猜测数据类型.此外,某些简单的功能(如事务处理)不能简单地通过简单的mysql函数完成.

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.

PDO可能是一个不错的选择,但是它的大多数驱动程序基本上都被废弃了,因此您在享受陷阱的同时却失去了与数据库无关的抽象层的好处.

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.

恕我直言,您所要求的事实表明您应该给mysqli一个机会.

IMHO, the mere fact that you are asking suggest that you should give mysqli a chance.

这篇关于切换到mysqli的一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆