类似查询的结果不同,为什么?

时间:2013-10-11 17:19:54

标签: php mysql sql

这个有点令人困惑,因为它在某些记录上完美运行,而在其他记录上则不然。我试图用特定的电子邮件提取所有记录,只在大约一半的案例中获得一条记录。我有两个查询集,一个拉数据,一个是拉一半的时间。 第一个一直有效的是:

$tiera = mysql_query("SELECT SUM(datamb) AS tiera FROM maindata2 WHERE dataplan = '2'  and    email='".mysql_real_escape_string($_POST['email'])."'");
$tiera1 = mysql_fetch_assoc($tiera); 
$tiera2 = $tiera1['tiera'];

$tieradata = mysql_query("SELECT SUM(dataplan) as datatotal FROM maindata2 WHERE dataplan =   '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tieradata1 = mysql_fetch_assoc($tieradata); 
$tieradata2 = $tieradata1['datatotal'];

echo "<table id='display1'>
            <tr>
                <th>Tier:</th>
                <th>Total Data in Tier:</th>
                <th>Data Usage This Period:</th>
                <th>Remaining:</th>

            </tr>";

echo "<tr>";
echo "<td>A</td> ";
echo "<td>". $tieradata2 . " MB</td> ";
echo "<td>". $tiera2. " MB</td> ";
echo "<td>".($tieradata1['datatotal'] - ROUND ($tiera1['tiera'],2)) . "MB</td></font> ";
echo "</table>";

我不能将它用于我的第二个查询,因为我在循环中需要它,如下所示:

$sql =  "SELECT phonenumber,date,email, dataplan AS currentplan, SUM(datamb) AS value_sum FROM maindata2 WHERE email='".mysql_real_escape_string($_POST['email'])."' GROUP BY dataplan, phonenumber ";


$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 Data Found For This Email";
    exit;
}

$row = mysql_fetch_assoc($result);



$query = mysql_query($sql) or die(mysql_error());


$header_printed = false;

while($row = mysql_fetch_array($query)) {
if ($row['phonenumber']) {
    if ($header_printed === false) {
        echo "   
            <table id='display'>
            <tr>
                <th>Phone Number:</th>
                <th>Data Plan:</th>
                <th>Data Usage This Period:</th>
                <th>Remaining:</th>
        <th>Date Reporting:</th>
            </tr>";
        $header_printed = true;
    }




while ($row = mysql_fetch_assoc($result)){
 echo "<tr>";
 echo "<td>".$row['phonenumber'] . "</td> ";
 echo "<td>".$row['currentplan'] . "MB</td> ";
 echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
 echo "<td><font color=$color>".($row['currentplan'] - ROUND ($row["value_sum"],2)) .    "MB</td></font> ";
  echo "<td>".$row['date'] . "</td></tr>";

 } 
}
echo "</table>";

所以问题是,第一个中的第二个查询缺少什么?

0 个答案:

没有答案