处理数据库查询的最佳方法是什么
我确信我当前所做的不是最佳的,但我真的很想听听一些关于如何处理 Web 应用程序上的所有数据库查询(包括连接字符串)的意见。
目前,我有一个 classes
目录,其中包含每个类的文件,还有另一个名为 db
的目录,其中包含 conn.php
其中包含连接字符串,db
目录中的其他文件类似于 classes
目录中的文件,每个类一个,但用于处理 mysql 查询。
所以基本上我将每个类的所有数据库查询都放在一个文件中,每当我需要从类文件中查询某些内容时,我都会调用数据库文件上的函数,
我将每个数据库文件包含在相应的数据库文件中,例如在 user.class.php
文件中,您将找到 include('db/user.db.php')
。
另外,我将 conn.php
文件包含到每个数据库文件中。
user.class.php
:
include('db/user.db.php');
class User {
public $fname;
public $userid;
function __construct($userid) {
$this->user_id = $userid;
$this->fname = DB_GetFirstName($userid);
}
}
user.db.php
:
include('conn.php');
function DB_GetFirstName($userid) {
$result = mysql_fetch_array(mysql_query("SELECT USR_FName FROM users WHERE USR_ID = '$userid'"));
return $result[0];
}
conn.php
:
$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("dbname", $conn);
你如何处理它?
I'm sure what i'm currently doing is not optimal, but I would really like to hear some opinions regarding how you should handle all db queries on a web application including the connection string.
At the moment, I have a classes
directory that has a file for each class and I have another directory called db
that includes a conn.php
which has the connection string and the other files on the db
directory are like in the classes
directory, one for each class but to handle the mysql queries.
So basically I have all db queries per class in one file and whenever I need to query something from the class file, I call the function on the db file
I include each db file in the corresponding db file, for example in the user.class.php
file you will find include('db/user.db.php')
.
Also, I include the conn.php
file to each db file.
user.class.php
:
include('db/user.db.php');
class User {
public $fname;
public $userid;
function __construct($userid) {
$this->user_id = $userid;
$this->fname = DB_GetFirstName($userid);
}
}
user.db.php
:
include('conn.php');
function DB_GetFirstName($userid) {
$result = mysql_fetch_array(mysql_query("SELECT USR_FName FROM users WHERE USR_ID = '$userid'"));
return $result[0];
}
conn.php
:
$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("dbname", $conn);
How do you handle it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用数据库抽象层。只要您验证输入并防止注入攻击,您所做的听起来似乎是比原始 SQL 行更好的方法。
流行的 PHP 框架,例如 Doctrine2 具有内置的数据库抽象层 这些内容已受到公众监督,并掩盖了许多您最终可能需要自己做的事情。
我建议在创建自己的层之前使用如上所述的开源层,因为已经为您制作了非常可靠的代码库。不要重新发明轮子。如果您发现项目存在缺陷,请考虑改进该项目。
Use a Database Abstraction Layer. What you have done sounds like it's a better approach than raw SQL lines, so long as you are validating input and preventing injection attacks.
Popular PHP frameworks such as Doctrine2 have built-in database abstraction layers which have been put under public scrutinized and cover off a lot of things you will probably end up doing yourself.
I advise to use an open-source layer such as mentioned above before creating your own, as there are very solid code bases already made for you. Don't re-invent the wheel. If you see a shortcoming, think about improving the project.
通过将域层与数据库访问层分离,您已经在做正确的事情了。但将它们组合在一起的方式可以改进。查看表数据网关 (TDG) 模式。
在表数据网关模式中,一个类封装了对特定表的所有访问。这有点像您的 User.Db.php,区别在于 TDG 是一个实际的类。您可以将任何相关的数据库访问分组到该特定类中,而不是一堆函数。这样做的直接好处是您可以将实例传递给任何需要它的类,而不是对它们进行硬编码函数调用。
每当您需要使用特定表时,您都可以使用 TDG 从中获取/修改行。然后您可以使用返回的记录集。或者使用 DataMapper 将记录集中的数据映射到您的 Domain 类,例如您的 User 类。对于简单的 DataMappers 来说,自己制作就可以了。一旦变得更加复杂,您最好使用现有的 ORM。
TDG 的替代方案是行数据网关模式。
有对 TDG 的很好的介绍(带有 Zend Framework 示例)
和行数据网关
You are doing the right thing already by separating your Domain Layer from the Database Access Layer. But the way you put it together could be improved. Have a look at the Table Data Gateway (TDG) pattern.
In the Table Data Gateway pattern one class encapsulates all the access to a specific table. This is kind of like your User.Db.php with the difference that the TDG is an actual class. Instead of a bunch of functions you group any related db access into that particular class. This has the immediate benefit that you can pass the instance to any class that needs it instead of hardcoding function calls into them.
Whenever you need to work with a specific table you use the TDG to fetch/modify rows from it. You can then either work with the returned recordsets. Or use a DataMapper to map the data from the recordset onto your Domain classes, e.g. your User class. For simple DataMappers crafting your own is fine. Once it gets more complex, you'll better of using an existing ORM.
An alternative to the TDG would be the Row Data Gateway pattern.
There is a good introduction to TDGs (with a Zend Framework example) at
and for Row Data Gateway
对于小型项目,我使用非常简单的 php 类 http://code.google.com/p /edb-php-class/
用于更广泛地使用数据库抽象层。
For small projects i use very simple php class http://code.google.com/p/edb-php-class/
for larger use a Database Abstraction Layer.