-1

我希望这些数据显示所有结果,在查询中我得到 129 个结果。但是当我在页面上显示它时,我只得到一行。我以前使用过非常相似的代码来获得多个结果,所以我知道这很简单,但我就是不明白。任何想法将不胜感激!

<?php

    $sql = "SELECT SUM(datamb) AS value_sum FROM maindata GROUP BY phonenumber";
$sql1 = "select dataplan as currentplan from maindata GROUP BY phonenumber";
$sql2 = "SELECT DISTINCT phonenumber AS value_sum1 FROM maindata";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

$result1 = mysql_query($sql2);
if (!$result1) {
    echo "Could not successfully run query ($sql1) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result1) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

$result2 = mysql_query($sql2);
if (!$result2) {
    echo "Could not successfully run query ($sql2) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result2) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while ($row = mysql_fetch_assoc($result)){
echo "<TABLE id='display'>";
echo "<td><b>Data Usage This Period: ". ROUND ($row["value_sum"],2) . "MB</b></td> ";
}
while ($row1 = mysql_fetch_assoc($result1)){
       echo "<TABLE id='display'>";
   echo "<td><b>Data Plan: ". $row1["currentplan"] . "</b></td> ";  
}
while ($row2 = mysql_fetch_assoc($result2)){    
   echo "<TABLE id='display'>";
   echo "<td><b>Phone Number: ". $row2["value_sum1"] . "</b></td> ";    
}
?> 

根据建议更新 - 非常有帮助,谢谢,我非常接近,所有值都是正确的,但我无法将它们放在同一张表中,想法?

4

3 回答 3

1

尝试使用,循环在你的代码中,例如。

   $result1 = mysql_query("SELECT DISTINCT phonenumber AS value_sum1 FROM maindata");

 echo '<table>';
 echo '<tr>';
 echo '<th>id</th>';
 echo '</tr>';

 while($record = mysql_fetch_assoc($result))
 {
 echo '<tr>';
 foreach ($record as $val)
{
   echo '<td>'.$val.'</td>';
}
 echo '</tr>';
}
echo '</table>';
于 2013-10-08T11:37:25.203 回答
0

添加

"LIMIT 0, 1" at the end of query

或者

编辑查询,如“从.....中选择 TOP 1”

于 2013-10-08T11:35:54.437 回答
0

让你轻松一点;)

看看这里如何使用 mysql_fetch_assoc

http://php.net/manual/en/function.mysql-fetch-assoc.php

按照示例进行操作,您将在几秒钟内完成。

于 2013-10-08T11:38:09.273 回答