只显示一行Mysql

时间:2013-10-08 11:30:27

标签: php mysql database

我希望这些数据显示所有结果,在查询中我得到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 个答案:

答案 0 :(得分: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>';

答案 1 :(得分:0)

添加

"LIMIT 0, 1" at the end of query

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

答案 2 :(得分:0)

让我们轻松一点;)

看看如何使用mysql_fetch_assoc

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

按照示例,您将在一秒钟内完成。

答案 3 :(得分:0)

要获取每一行,您需要将其放入while循环中。不确定它是否会帮助你。

echo "<TABLE id='display'>"; 
 while ($row = mysql_fetch_array($result1)) { 
 echo "<tr>";
 echo "<td><b>Phone Number: ". $row['field_name'] . "</b></td> ";
 echo "<td><b>Data Plan: ". $row['field_name'] . "MB</b></td> ";
 echo "<td><b>Data Usage This Period: ". ROUND ($row['field_name'],2) . "MB</b></td> ";
 echo "<td><b>Data Remaining: ". ROUND ($row['field_name'],2) . "MB</b></td> ";
echo "</tr>";
} 
 echo "</table>"; 
 echo "<br>";