我一直在努力从mysql表填充php上的数据透视表。我在谷歌上得到了以下代码,但我想这个代码有问题
Php CODE:
<!DOCTYPE html>
<html>
<body>
<?php
$q = ($_GET['q']);
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM ( SELECT Designation, Department FROM headcount)t PIVOT (Count (Department) FOR Department IN $q) AS pvt";
mysql_select_db('emp_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
echo "Designation :{$row['Designation']} <br> ".
"Department : {$row['$q']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
错误:
Could not get data: 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 'PIVOT (Count (Department) FOR Department IN VOC) AS pvt' at line 1
答案 0 :(得分:0)
您收到的错误表明执行SQL查询时出现了问题。
在SQL语句中,尝试删除粗体部分并再次执行php脚本。
$ sql =“SELECT * FROM(SELECT Designation,Department FROM 人数) t PIVOT(计数(部门)FOR部门IN $ q)AS PVT“;