简体   繁体   English

PHP函数用于处理MySQL无限和嵌套查询

[英]PHP function for handling MySQL infinite & nested queries

I've been asked by my colleague to make a PHP function to plug into our web application to handle an infinite amount of nested (looped) queries to make our lives much easier, without being worried about loosing the current and/or previous results. 我的同事曾要求我制作一个PHP函数以插入到我们的Web应用程序中,以处理无限数量的嵌套(循环)查询,从而使我们的生活更加轻松,而不必担心失去当前和/或以前的结果。

Here is the code I came up after minutes, and it seems that it works fine, however I still have these questions: 这是我几分钟后出现的代码,看来工作正常,但是我仍然有以下问题:

  1. Am I re-inventing the mysqli_prepare function? 我是否在重新发明mysqli_prepare函数?
  2. Is it smart to handling these nested queries in this way? 以这种方式处理这些嵌套查询是否明智?
  3. What could be pros and cons of using the following approach? 使用以下方法可能有什么优缺点?

The actual function: 实际功能:

function qn($query) {
    global $db;
    $rand_var = 'r' . mktime() . mt_rand();
    $$rand_var = $db->query($query);
    return $$rand_var;
}

and in action: 并采取行动:

if (($db instanceof mysqli) != true) {
   $db = new mysqli(DB_ADDRESS, DB_USER, DB_PASS, DB_NAME);
}

$a = qn('SELECT DISTINCT ***');

while ($row_a = $a->fetch_assoc()) {
    // do some stuff
    $b = qn('SELECT ***' . $row_a['foo']);
    while ($row_b = $b->fetch_assoc()) {
        $c = qn('SELECT COUNT(id)' . $row_b['bar']);
        // keep going ...
    }
}

Note: SQL queries are sample. 注意: SQL查询是示例。

Here you go: SafeMysql is what you're looking for and many, many more! 在这里,您可以找到: SafeMysql是您正在寻找的东西,还有更多!

if (!($db instanceof safemysql)) {
   $db = new safemysql(...);
}

$a = $db->getCol('SELECT DISTINCT ***');
foreach ($a as $foo) {
    // do some stuff
    $b = $db->getAll('SELECT ***', $foo);
    foreach ($b as $row_b) {
        $c = $db->getOne(('SELECT COUNT(id)', $row_b['bar']);
         // keep going ...
    }
}

To answer your question 回答你的问题

Am I re-inventing the mysqli_prepare function? 我是否在重新发明mysqli_prepare函数?

Definitely NO. 绝对没有
You're preparing nothing. 你什么都没准备。

Is it smart to handling these nested queries in this way? 以这种方式处理这些嵌套查询是否明智?

No. The probability of collision is pretty high. 不会。发生碰撞的可能性很高。 At least use microtime instead of mktime. 至少使用microtime代替mktime。

What could be pros and cons of using the following approach? 使用以下方法可能有什么优缺点?

There a lot of cons 有很多缺点

  • no error handling 没有错误处理
  • no placeholders support 没有占位符支持
  • messy code 凌乱的代码
  • whole qn() function being useless. 整个qn()函数无效。 you can use $db->query() alone instead. 您可以单独使用$ db-> query()。

Also, Till Helge Helwig is absolutely right: all nested queries can (and should) be replaced with single query with join (and grouping). 同样, Till Helge Helwig绝对正确:所有嵌套查询都可以(并且应该)替换为带有连接(和分组)的单个查询。

The following code: 如下代码:

$rand_var = 'r' . mktime() . mt_rand();
$$rand_var = $db->query($query);
return $$rand_var;

could be simplified to 可以简化为

return $db->query($query);

Cause it just stores query result in local variable with unique name before returning it. 因为它只将查询结果存储在具有唯一名称的局部变量中,然后返回。 This does not make much sense. 这没有多大意义。

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

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