Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP实现MySQL动态数据透视表_Php_Mysql_Dynamic_Pivot - Fatal编程技术网

用PHP实现MySQL动态数据透视表

用PHP实现MySQL动态数据透视表,php,mysql,dynamic,pivot,Php,Mysql,Dynamic,Pivot,我使用MySQL Workbench实现了以下动态pivot。但是,我在使用PHP执行它时遇到了问题。在过去的7天里,我试了很多次,但都不管用。需要了解PHP代码应该是什么样子才能运行它 SET @sql = NULL; SET group_concat_max_len=15000; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'COUNT(IF(int_supplierid = ', int_supplierid,

我使用MySQL Workbench实现了以下动态pivot。但是,我在使用PHP执行它时遇到了问题。在过去的7天里,我试了很多次,但都不管用。需要了解PHP代码应该是什么样子才能运行它

SET @sql = NULL;
SET group_concat_max_len=15000;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'COUNT(IF(int_supplierid = ',
      int_supplierid,
      ', int_projectid, NULL)) AS NO',
      int_supplierid
    )
  ORDER BY int_supplierid) INTO @sql
FROM tbl_po;
SET @sql = CONCAT('SELECT int_userid, ', @sql, ' FROM tbl_po GROUP BY int_userid');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
我使用的第一个php代码

<?php
require_once('myconn.php');

$approval="SET @sql = NULL;
SET group_concat_max_len=15000;"

$approval3="
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'COUNT(IF(int_supplierid = ',
      int_supplierid,
      ', int_projectid, NULL)) AS NO',
      int_supplierid
    )
  ORDER BY int_supplierid) INTO @sql
FROM tbl_po;
SET @sql = CONCAT('SELECT int_userid, ', @sql, ' FROM tbl_po GROUP BY int_userid');
PREPARE stmt FROM @sql;
EXECUTE stmt;"

$RSFeed2 = mysql_query($approval) or die(mysql_error());
$RSFeed2 = mysql_query($approval3) or die(mysql_error());
$totalRows_RSFeed = mysql_num_rows($RSFeed2);
echo $RSFeed2['int_userid'];
echo $totalRows_RSFeed;
?>

PHP的mysql API不支持多查询

换句话说,您不能准备和执行包含多个以分号分隔的SQL语句的字符串

同样,mysqli不支持使用普通mysqli::query()函数进行多查询。你必须使用

但多查询是一个很好的选择。您应该坚持逐个执行语句

PS:mysql扩展是从PHP5.3.0开始的,因此您应该在任何新代码中使用mysqli或PDO


我从你们的例子中看到,你们试图做一个动态的轴心。您希望每个supplierid对应一列,该列的值应为该supplierid的计数

将这些计数作为行而不是列输出更简单、更高效:

$query = "SELECT int_supplierid, COUNT(int_projectid) 
    FROM tbl_po GROUP BY int_supplierid";

if (!($stmt = $con->prepare($query))) {
    die($con->error);
}
if (!$stmt->execute()) { 
    die($stmt->error); 
}
$stmt->bind_result($int_supplierid, $no);
while ($stmt->fetch()) {
    printf("%s: %d\n", $int_supplierid, $no);
}

您可能想发布一些用于执行此查询的PHP代码。这是一个存储过程吗?有什么不起作用?它会抛出错误吗?感谢所有回复。包含php代码编辑。对不起,比尔。我试了又试。出去休息一下。重复一遍又一遍。我好像弄不明白。这里真的需要帮助吗
$query = "SELECT int_supplierid, COUNT(int_projectid) 
    FROM tbl_po GROUP BY int_supplierid";

if (!($stmt = $con->prepare($query))) {
    die($con->error);
}
if (!$stmt->execute()) { 
    die($stmt->error); 
}
$stmt->bind_result($int_supplierid, $no);
while ($stmt->fetch()) {
    printf("%s: %d\n", $int_supplierid, $no);
}