构建 ORM - mysqli 与常规 SQL 与 PDO

发布于 2024-10-28 09:08:35 字数 738 浏览 2 评论 0原文

我一直在寻找一个非常轻量级的 PHP ORM 库,并且遇到了一些不错的选择。不幸的是,大多数选择都需要某种形式的 配置,例如向类中添加更多字段以及其中的许多字段 不太直观。

我不想使用框架,但更喜欢 Q&D 的 ORM 原型。

不管怎样,我开始构建自己的 PHP ORM 库,称为 SORM(简单 ORM)。它很大程度上处于阿尔法状态。 查看代码和示例,

您可以在https://github.com/teraom/sorm

我将不胜感激如果您可以提供一些反馈或指示 如何进行、需要改变的事情等 我有兴趣使用 mysqli 进行数据库查询,特别是准备好的 语句,以避免SQL注入。

  1. 有没有办法在常规 PHP - SQL 中使用准备好的语句 功能?或者通过其他方式防止SQL注入? https://www.php.net/manual/en/ref.mysql。 php

  2. 安装PHP时默认安装了mysqli吗? (PHP 5.2 和 上)

  3. 我对 PDO 知之甚少。 PHP 5.2及以上版本默认安装PDO吗? PDO 也会出现吗?

谢谢, 巴拉德

I have been looking for a very light weight PHP ORM library and I ran across a few good choices. Unfortunately, most choices require some form of
configuration, e.g. adding more fields to the class and many of them
are not so intuitive.

I do not want to use a framework, but prefer just the ORM for Q&D
prototypes.

Anyway, I started building my own PHP ORM library called SORM (Simple ORM). It is very much in alpha state. You can checkout the code and examples at

https://github.com/teraom/sorm

I would appreciate if you can provide some feedback or directions on
how to proceed, things to change etc.
I am interested in using mysqli for database queries, specifically for prepared
statements, so as to avoid SQL injection.

  1. Is there a way to use prepared statements with regular PHP - SQL
    functions? Or prevent SQL injection by other means?
    https://www.php.net/manual/en/ref.mysql.php

  2. Is mysqli installed by default when you install PHP? (PHP 5.2 and
    above)

  3. I know very little about PDO. Is PDO installed by default in PHP 5.2 and above? Does PDO come into picture too?

Thanks,
Bharad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2024-11-04 09:08:35

有没有办法将准备好的语句与常规 PHP - SQL 函数一起使用?

仅当常规功能支持时。过时的“mysql”扩展不提供此功能。其他一些则这样做,例如 PostgreSQL 的。

安装PHP时默认安装mysqli吗? (PHP 5.2 及以上)

这是无法回答的,因为它取决于 PHP 的编译方式(如果是自定义安装),或者是否通过包管理器安装。如果它是通过包管理器安装的,那么所有数据库支持很可能都在可选附加包中。但是,如果启用了 mysql 支持,则“mysqli”扩展应该可用。

PHP 5.2 及以上版本是否默认安装 PDO?

与之前的答案相同——这取决于 PHP 的安装方式。它在编译时默认启用,但根据包管理的不同,它可能不可用。

一般来说,您可以依赖 PDO 在大多数情况下都可用。如果不是,则要么是系统管理员的监督,要么是白痴经理故意决定禁用它。

许多现代 PHP 都是假设 PDO 存在而构建的。为 PDO 和 mysqli 制作适配器并没有太大的坏处,因为两者都具有大体相似的功能集。请注意 bind_param< 的奇怪方式/code>有效 - 它需要一次调用所有要绑定的内容,通过引用。这往往会让人们失去兴趣。


另一种选择:不要直接在 PDO 之上构建,而是在另一个包装器之上构建。我不想让您了解所有 Inception,但请看一下 Zend_Db< /a>.是的,我知道你不想使用框架,但无论如何还是看一下它。它具有适用于各种 PDO 风格、mysqli、Oracle 和 DB2 的适配器。它也非常全面,并且查询构建器(Zend_Db_Select)非常方便。

该领域还有很多其他选项,例如 好旧的 PEAR MDB2,它使用 < a href="http://pear.php.net/manual/en/package.database.mdb2.intro-execute.php" rel="nofollow">模拟伪造准备好的语句和占位符。

Is there a way to use prepared statements with regular PHP - SQL functions?

Only when supported by the regular functions. The decrepit "mysql" extension does not provide this functionality. Some others do, like the one for PostgreSQL.

Is mysqli installed by default when you install PHP? (PHP 5.2 and above)

This is not answerable, as it depends on how PHP was compiled (if it's a custom installation), or whether it was installed via a package manager. If it was installed through a package manager, then it's very likely that all database support might be in an optional add-on package. However, if mysql support in general is enabled, the "mysqli" extension should be available.

Is PDO installed by default in PHP 5.2 and above?

Same answer as before -- it depends on how PHP was installed. It's enabled by default when compiling, but it might not be available depending on package management.

In general, you can rely on PDO being available most of the time. When it's not, it's either going to be oversight on behalf of the sysadmin, or an intentional decision to disable it by an idiot manager.

A lot of modern PHP is built assuming PDO will be there. There's not too much harm in making adapters for both PDO and mysqli, as both have generally similar feature sets. Just watch out for the weird, weird way that bind_param works -- it expects one call with all of the things to bind, by reference. This tends to turn people off.


Another option: Instead of building on top of PDO directly, build on top of another wrapper. I don't want to get all Inception on you, but take a peek at Zend_Db. Yes, I know you don't want to use a framework, but take a look at it anyway. It has adapters for the various PDO flavors, mysqli, Oracle, and DB2. It's also quite comprehensive, and the query builder (Zend_Db_Select) is pretty handy.

There are lots of other options in this area, like good old PEAR MDB2, which uses emulation to fake prepared statements and placeholders.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文