0

I want to display a table that looks like this from a Mysql query using PHP:

Vendor Name

Item Not to Drawing     Item Defective    Incorrect Item Received   Other

         9                    2                    3                  5


Vendor Name

Item Not to Drawing     Item Defective    Incorrect Item Received   Other

         2                    4                    5                  7

etc..

Here is my code....

<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("qa", $con);

$result = mysql_query("SELECT Vendor, Sum(draw), Sum(defective), Sun(received), Sum(other) FROM qa_reports Group by Vendor Order by Vendor ASC");
echo "<table>
<tr>
<th>Item not to Drawing</th>
<th>Item Defective</th>
<th>Incorrect Item Received</th>
<th>Other</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Sum(draw)'] . "</td>";
  echo "<td>" . $row['Sum(defective)'] . "</td>";
  echo "<td>" . $row['Sum(received'] . "</td>";
  echo "<td'>" . $row['Sum(other)'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

Not sure how to get the above results formatted as such.

I've done this with CFML but am new to PHP and can not grasp how to list results in a table grouped by a field.

4

2 回答 2

2

您需要为列设置别名以便在$row数组中引用它们。

SELECT vendor, 
       Sum(draw)      AS draw, 
       Sum(defective) AS defective, 
       Sun(received)  AS received, 
       Sum(other)     AS other 
FROM   qa_reports ...

然后你可以像这样引用它们:

$row['draw'];
...
于 2013-02-11T18:13:44.797 回答
2

SELECT Vendor, Sum(draw) AS sumDraw, Sum(defective) AS sumDefective...

$row['sumDraw']等等

于 2013-02-11T18:15:04.803 回答