数据包装方法

发布于 2024-10-11 02:52:15 字数 622 浏览 3 评论 0原文

我正在使用 PHP 和 MySQL 进行开发。

有关当前登录用户的信息存储在许多不同的表中。我预先加载了每个页面上所需的信息。但是,如果需要从很少访问的表中获取某些内容,那么我

$newdata = $db->Query('SELECT * FROM rare_table WHERE user_id='.$user->id);

想将上述内容简化到不必指定查询应仅限于该特定用户的程度。理想的函数调用是:

$newdata = $user->Query('SELECT * FROM rare_table');

显然我必须解析 SQL 并添加 WHERE 子句。或者添加到已经存在的条款中。

问题:有工具可以做到这一点吗?我该如何开发这个?这是个好主意吗?


编辑:找到SQL 解析器 可以帮助那些对此感兴趣的人。

I'm developing in PHP and MySQL.

The information about the currently logged in user is stored in many different tables. The information that I need on each page, I preload. However if something is needed from a rarely accessed table - then I do

$newdata = $db->Query('SELECT * FROM rare_table WHERE user_id='.$user->id);

I would like to simplify the above to a point where I don't have to specify that the query should be limited to this particular user. An ideal function call would be:

$newdata = $user->Query('SELECT * FROM rare_table');

Obviously I'd have to parse the SQL and add a WHERE clause. Or add to the already existing clause.

Questions: are there tools to do this? How can I develop this? Is this even a good idea?


Edit: Found an SQL parser that may assist those interested in doing this.

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

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

发布评论

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

评论(2

枕花眠 2024-10-18 02:52:15

看起来您想向用户添加一个功能

public function getRow($tableName)
{
     $sql = "SELECT * FROM $tableName WHERE user_id = " . $this->$id;
     //run query

问题是获取 $db,它属于用户吗?你有一个函数可以得到它吗?

public function getRowFrom($tableName)
{
     $sql = "SELECT * FROM $tableName WHERE user_id = " . $this->$id;
     $db = getDb(); //however you need to do this.  $this->db, DBTools::getDB(), call constructor here, etc.
     return $db->Query($sql);
}

并将其称为

$user->getRowFrom('rareTableNo4');

Looks like you want to add a function to user

public function getRow($tableName)
{
     $sql = "SELECT * FROM $tableName WHERE user_id = " . $this->$id;
     //run query

The issue is getting $db, does it belong to user? do you have a function to get it?

public function getRowFrom($tableName)
{
     $sql = "SELECT * FROM $tableName WHERE user_id = " . $this->$id;
     $db = getDb(); //however you need to do this.  $this->db, DBTools::getDB(), call constructor here, etc.
     return $db->Query($sql);
}

and call it as

$user->getRowFrom('rareTableNo4');
暮光沉寂 2024-10-18 02:52:15

我通常创建一个名为 User 的类,并为每个页面实例化一个单例(使用登录用户)。可以为其他用户创建额外的实例(例如 $u2 = new User($name);),然后这些东西就会嵌入到 User 中。

I generally create a class called User and instantiate a singleton for each page (with the logged-in user.). Additional instances can be created for other users (e.g. $u2 = new User($name);) Then such things get embedded in User.

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