你能帮助我在动态数据透视表上使用这段代码。错误:(!)致命错误:调用成员函数fetch_all()

时间:2018-06-13 07:06:57

标签: php dynamic pivot-table

<?php include("db.php");?>
<?php
$recno=$_GET['recno'];
$con->query('SET @sql = NULL');
$con->query("SELECT\n"
    . "  GROUP_CONCAT(DISTINCT\n"
    . "    CONCAT(\n"
    . "      \'MAX(IF(parid = \'\'\',\n"
    . "      parid,\n"
    . "      \'\'\', par_values, 0)) AS \'\'\',\n"
    . "      parid,\'\'\'\'\n"
    . "    )\n"
    . "  ) INTO @sql\n"
    . "FROM\n"
    . "  est_paaram_value\n"
    . "WHERE recno=\'132\'");
$con->query("SET @sql = CONCAT(\'SELECT recno,year,qtr, \', @sql, \' FROM est_paaram_value WHERE recno='132' GROUP BY year,qtr\')");
$con->query("PREPARE stmt FROM @sql");
$res = $con->query("EXECUTE stmt");
var_dump($res->fetch_all(MYSQLI_ASSOC));
?>
  

(!)致命错误:在第46行的C:\ wamp \ www \ SME \ querying.php中调用boolean上的成员函数fetch_all()

没有显示结果。

1 个答案:

答案 0 :(得分:0)

正如 u_mulder Jeff 所提到的,没有必要在双引号字符串中转义单引号,而且也有太多。

查询中出现错误,导致$con->query("EXECUTE stmt");失败并返回 false ,然后将其分配给 $ res

清理查询,删除所有不必要的单引号和转义符,并在执行失败时添加输出,如下所示:

<?php
include("db.php");

$recno=$_GET['recno'];
$con->query('SET @sql = NULL');
$con->query("SELECT\n"
    . "  GROUP_CONCAT(DISTINCT\n"
    . "    CONCAT(\n"
    . "      MAX(IF(parid = '',\n"
    . "      parid,\n"
    . "      ``, par_values, 0)) AS ``,\n"
    . "      parid,''\n"
    . "    )\n"
    . "  ) INTO @sql\n"
    . "FROM\n"
    . "  est_paaram_value\n"
    . "WHERE recno='132'");
$con->query("SET @sql = CONCAT('SELECT recno,year,qtr, ', @sql, ' FROM est_paaram_value WHERE recno='132' GROUP BY year,qtr')");
$con->query("PREPARE stmt FROM @sql");
$res = $con->query("EXECUTE stmt");
if ($res) {
    var_dump($res->fetch_all(MYSQLI_ASSOC));
} else {
    echo "\nPDO::errorCode(): ", $con->errorCode();
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}

注意:上面显示的代码可能仍然无效,因为没有关于数据库架构的信息或您实际想要查询的内容。但它应该为您提供有关查询失败的信息以及错误代码和信息。