简体   繁体   English

什么是处理数据库查询的最佳方法

[英]What is the best approach to handling db queries

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. 我确定我当前所做的并不是最佳选择 ,但是我真的很想听听您关于如何处理Web应用程序上所有数据库查询(包括连接字符串)的意见。

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. 目前,我有一个classes目录,其中每个类都有一个文件,并且我还有另一个名为db目录,其中包括一个具有连接字符串的conn.php ,而db目录中的其他文件就像在classes目录中一样。对于每个类,但要处理mysql查询。

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 因此,基本上我在一个文件中拥有每个类的所有数据库查询,并且每当需要从类文件中查询某些内容时,我都会在db文件中调用该函数

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') . 我将每个数据库文件都包含在相应的数据库文件中,例如,在user.class.php文件中,您会发现include('db/user.db.php')

Also, I include the conn.php file to each db file. 另外,我将conn.php文件包含到每个db文件中。

user.class.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 : 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.php

$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("dbname", $conn);

How do you handle it? 您如何处理?

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. 只要您正在验证输入并防止注入攻击,完成的操作听起来比原始SQL行更好。

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. 流行的PHP框架(例如Doctrine2)具有内置的数据库抽象层 ,这些已受到公众审查,并且掩盖了许多您可能最终会自己做的事情。

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. 如果发现不足之处,请考虑改进该项目。

For small projects i use very simple php class http://code.google.com/p/edb-php-class/ 对于小型项目,我使用非常简单的php类http://code.google.com/p/edb-php-class/

$result = $db->q("select * from `users`limit 3");

foreach($result as $a){
        echo $a['name'].' '.$a['surname'].' '.$a['email'].' '.$a['country'].'</br>';
}

for larger use a Database Abstraction Layer. 对于更大的用途,数据库抽象层。

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. 看一下表数据网关 (TDG)模式。

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. 这有点像您的User.Db.php,不同之处在于TDG是实际的类。 Instead of a bunch of functions you group any related db access into that particular class. 您可以将所有相关的db访问权限分组到该特定类中,而不是一堆函数。 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. 每当需要使用特定表时,都可以使用TDG从中获取/修改表中的行。 You can then either work with the returned recordsets. 然后,您可以使用返回的记录集。 Or use a DataMapper to map the data from the recordset onto your Domain classes, eg your User class. 或使用DataMapper将记录集中的数据映射到您的Domain类(例如,User类)上。 For simple DataMappers crafting your own is fine. 对于简单的DataMappers,您可以自己制作。 Once it gets more complex, you'll better of using an existing ORM. 一旦变得更加复杂,最好使用现有的ORM。

An alternative to the TDG would be the Row Data Gateway pattern . TDG的替代方法是行数据网关模式

There is a good introduction to TDGs (with a Zend Framework example) at 可以在以下位置很好地介绍TDG(以Zend Framework为例)。

and for Row Data Gateway 和行数据网关

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

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