我使用 MySQL Workbench 进行了以下动态枢轴工作。然而,我有使用 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;
?>
我使用并尝试过的第二个;
$host="127.0.0.1";
$port=3306;
$socket="";
$user="root";
$password="pwd";
$dbname="kelupis";
$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
//1st query
$query1 = "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;";
if ($stmt = $con->prepare($query1)) {
$stmt->execute();
$stmt->bind_result($int_supplierid);
while ($stmt->fetch()) {
printf("%s\n", $int_supplierid);
}
}