mySQL 句子的问题包括两个临时 table 创建

Issue with an mySQL sentence including two temporary table creation

我有一个 mySQL 句子,如果我在我的 phpMyAdmin 中执行它,它就像一个魅力:

CREATE TEMPORARY TABLE hash1
      SELECT * FROM
      (
        (
            SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '1' AND feature_value = 'No frost total'
        ) UNION 
        (
            SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '3' AND feature_value = '43'
        )) AS q;


      CREATE TEMPORARY TABLE hash2
        SELECT * FROM hash1;

        SELECT 
          p.id AS id, 
          p.main_image AS main_image, 
          p.type AS taxonomy, 
          p.name AS model, 
          p.sku AS sku, 
          p.price AS price, 
          b.brand_name AS brand_name, 
          b.brand_image AS brand_logo,
          pf.feature_value AS feature_value, 
          f.feature AS feature_label,
          f.id AS feature_id
        FROM
        (
          SELECT  a.*
          FROM    gf_product AS a
          INNER JOIN
          (
            SELECT product_id
            FROM
            (
              SELECT a.product_id , count(*) AS commons
              FROM   gf_product_features AS a
              INNER JOIN hash1 AS b 
                ON    a.feature_id = b.fl 
                AND   a.feature_value = b.fv 
              GROUP BY a.product_id 
              ) AS features
              WHERE commons = (SELECT count(*) AS count FROM hash2)  
            ) b1 ON a.id = b1.product_id 
          ) AS p
        INNER JOIN  gf_brands AS b 
            ON p.brand_id = b.id
        INNER JOIN  gf_product_features AS pf 
            ON pf.product_id = p.id   
        INNER JOIN  gf_features AS f 
            ON pf.feature_id = f.id
        ORDER BY    price ASC, 
                    feature_id ASC

我想通过 Ajax 请求执行一个 php 函数,它动态地构造上面的 sql 句子,但我总是在浏览器的控制台中收到此错误:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TEMPORARY TABLE hash2
        SELECT * FROM hash1;

        SELECT 
       ' at line 12

因此,也出现以下错误:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /www/htdocs/example/inc/functions.php on line 538

对应于我的php代码的这一行:

while ($row = mysqli_fetch_assoc($result))

也许从 hash1 table

克隆 hash2 table
CREATE TEMPORARY TABLE hash2
        SELECT * FROM hash1;

听起来很奇怪,但如果我不那样做,在我的 phpMyAdmin 中我会得到这个错误:

 #1137 - Can't reopen table: 'b'

我不明白为什么我的 sql 句子在我的 phpMyadmin 中工作正常,但是当我在我的 php 文件中构建它时它不起作用。有人可以帮帮我吗?

有关更多信息,这是我的 PHP 代码:

    function getProductsFromFilteredQuery($connection, $filters, &$html)
{
    $sql = '';
    $m = count($filters); // $filters are an array of values like this: ['value1A, value2A', 'value1B, value2B', ...]

    $sql = 'CREATE TEMPORARY TABLE hash1
      SELECT * FROM
      (';

    for ($n = 0; $n < $m; $n++)
    {
        $string                 = explode(', ', $filters[$n]);
        $feature_id         = $string[0];
        $feature_value  = $string[1];

        $sql .= "
        (
            SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '" . $feature_id . "' AND feature_value = '" . $feature_value . "'
        )";

        if ($n < ($m - 1))
        {
            $sql .= ' UNION ';
        }
    }


    $sql .=  ') AS q;


      CREATE TEMPORARY TABLE hash2 -- In this line I get an error
        SELECT * FROM hash1;

        SELECT 
          p.id AS id, 
          p.main_image AS main_image, 
          p.type AS taxonomy, 
          p.name AS model, 
          p.sku AS sku, 
          p.price AS price, 
          b.brand_name AS brand_name, 
          b.brand_image AS brand_logo,
          pf.feature_value AS feature_value, 
          f.feature AS feature_label,
          f.id AS feature_id
        FROM
        (
          SELECT  a.*
          FROM    gf_product AS a
          INNER JOIN
          (
            SELECT product_id
            FROM
            (
              SELECT a.product_id , count(*) AS commons
              FROM   gf_product_features AS a
              INNER JOIN hash1 AS b 
                ON    a.feature_id = b.fl 
                AND   a.feature_value = b.fv 
              GROUP BY a.product_id 
              ) AS features
              WHERE commons = (SELECT count(*) AS count FROM hash2)  
            ) b1 ON a.id = b1.product_id 
          ) AS p
        INNER JOIN  gf_brands AS b 
            ON p.brand_id = b.id
        INNER JOIN  gf_product_features AS pf 
            ON pf.product_id = p.id   
        INNER JOIN  gf_features AS f 
            ON pf.feature_id = f.id
        ORDER BY    price ASC, 
                    feature_id ASC';

    $result = mysqli_query($connection, $sql);

    while ($row = mysqli_fetch_assoc($result)) // In this line I get an error too
    {
        // Do some stuff... and at last, return the resulting $html
    }
};

我终于找到错误了。在我的 phpMyAdmin 中,它工作得很好,因为有人可以在 SQL 控制台中执行多个查询。没问题。

但是,当通过 PHP 编码 mySQL 查询时,您一次只能 运行 一个 mySQL 句子。好吧,有一个例外:您可以使用 mysqli_multi_query + mysqli_more_results 或类似的东西。但是正如我编写的那样,你不能。

所以有两个选择:重写 PHP 代码,就像上面两个链接的页面中描述的那样,或者在 PHP 函数中做几个 mysqli_query

我决定通过第二个选项来做,所以工作代码如下(注意每个mysqli_query后面的注释):

function getProductsFromFilteredQuery($mysqli, $filters, &$html) {
$sql = '';
$m = count($filters);

$sql        = 'DROP TEMPORARY TABLE IF EXISTS hash1;';
$result = mysqli_query($mysqli, $sql); // A single query

$sql        = 'DROP TEMPORARY TABLE IF EXISTS hash2;';
$result = mysqli_query($mysqli, $sql); // Another single query

$sql        = 'CREATE TEMPORARY TABLE hash1
  SELECT * FROM
  (';

for ($n = 0; $n < $m; $n++)
{
    $string                 = explode(', ', $filters[$n]);
    $feature_id         = $string[0];
    $feature_value  = $string[1];

    $sql .= "
    (SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '" . $feature_id . "' AND feature_value = '" . $feature_value . "')";

    if ($n < ($m - 1))
    {
        $sql .= ' UNION ';
    }
}


$sql .=  ') AS q1';
$result = mysqli_query($mysqli, $sql); // Another single query

$sql =  'CREATE TEMPORARY TABLE hash2
    SELECT * FROM hash1;';
$result = mysqli_query($mysqli, $sql);  // Another single query

$sql = 'SELECT 
              p.id AS id, 
              p.main_image AS main_image, 
              p.type AS taxonomy, 
              p.name AS model, 
              p.sku AS sku, 
              p.price AS price, 
              b.brand_name AS brand_name, 
              b.brand_image AS brand_logo,
              pf.feature_value AS feature_value, 
              f.feature AS feature_label,
              f.id AS feature_id
            FROM
            (
              SELECT  a.*
              FROM    gf_product AS a
              INNER JOIN
              (
                SELECT product_id
                FROM
                (
                  SELECT a.product_id , count(*) AS commons
                  FROM   gf_product_features AS a
                  INNER JOIN hash1 AS b 
                    ON    a.feature_id = b.fl 
                    AND   a.feature_value = b.fv 
                  GROUP BY a.product_id 
                  ) AS features
                  WHERE commons = (SELECT count(*) AS count FROM hash2)  
                ) b1 ON a.id = b1.product_id 
              ) AS p
            INNER JOIN  gf_brands AS b 
                ON p.brand_id = b.id
            INNER JOIN  gf_product_features AS pf 
                ON pf.product_id = p.id   
            INNER JOIN  gf_features AS f 
                ON pf.feature_id = f.id
            ORDER BY    price ASC, 
                        feature_id ASC';

$result = mysqli_query($mysqli, $sql);  // Another single query. The last one.

while ($row = mysqli_fetch_assoc($result))
{
    // My stuff here...
}
}; // @END of function