使用select语句在SQL中打印PHP表

时间:2013-12-12 08:43:30

标签: php mysql html-table

在程序中,我想创建表格以(Y / M / D)格式,音量,待定和发送的日期打印出来。但是,执行后它没有显示任何内容。它可以在SQL中使用phpmyadmin执行。但是,我不知道我们是否可以使用两个SQL语句来找出两个不同的条件。我需要你的帮助。

$start = date('2013-12-12');
$today = date("Y/m/d");
echo "Today's date: " .$today;
$result = mysql_query("SELECT count(semail) AS av FROM table WHERE `thedate`>='2013-12-12' GROUP BY `thedate` ")or die(mysql_error());  
$row = mysql_fetch_assoc($result) or die("No rows returned by query"); 
$totalofemail = $row['av'];
$sent = mysql_query("SELECT count(semail) AS av FROM table WHERE `sflag`='1' AND `thedate`>='2013-12-12' GROUP BY `thedate`")or die(mysql_error());  
$result = mysql_fetch_assoc($sent) or die("No rows returned by query");  
$sentemail = $result['av'];
$pending = $totalofemail - $sentemail;

echo "<table border='1'>
      <tr>
        <th>Date</th>
        <th>Daily volume</th>
        <th>Sent</th>
        <th>Pending</th>
      </tr>";

if($i=0; $i<($today-$start); $i++)
{
   echo "<tr>\n";
   echo "<td>" . $row['thedate'] . "</td>\n";
   echo "<td>" . $totalofemail . "</td>\n";
   echo "<td>" . $sentemail . "</td>\n";
   echo "<td>" . $pending . "</td>\n";
   echo "\n</tr>\n";
   echo "</table>\n";
}

2 个答案:

答案 0 :(得分:3)

错误原因为if($i=0; $i< ($today-$start); $i++)

但是在您的代码中,不需要使用for循环

尝试使用,

for($i=0; $i< ($today-$start); $i++)

而不是

if($i=0; $i< ($today-$start); $i++)

代码:删除了循环条件。因为循环没有必要。

$start = date('2013-12-12');
$today = date("Y/m/d");
echo "Today's date: " .$today;
$result = mysql_query("SELECT count(semail) AS av FROM mycard WHERE `thedate`>='2013-12-12' GROUP BY `thedate` ")or die(mysql_error());  
$row = mysql_fetch_assoc($result) or die("No rows returned by query"); 
$totalofemail = $row['av'];


$sent = mysql_query("SELECT count(semail) AS av FROM mycard WHERE `sflag`='1' AND `thedate`>='2013-12-12' GROUP BY `thedate`")or die(mysql_error());  
$result = mysql_fetch_assoc($sent) or die("No rows returned by query");  
$sentemail = $result['av'];


$pending = $totalofemail - $sentemail;

echo "<table border='1'>
      <tr>
        <th>Date</th>
        <th>Daily volume</th>
        <th>Sent</th>
        <th>Pending</th>
      </tr>";

      echo "<tr>\n";
      echo "<td>" . $row['thedate'] . "</td>\n";
      echo "<td>" . $totalofemail . "</td>\n";
      echo "<td>" . $sentemail . "</td>\n";
      echo "<td>" . $pending . "</td>\n";
      echo "\n</tr>\n";


   echo "</table>\n";

答案 1 :(得分:1)

你想做什么?一个新的视角?

    if($i=0; $i<($today-$start); $i++)

不应该是for吗?

for($i=0; $i<($today-$start); $i++)

另一个“无聊”的部分是关于使用mysql_ *函数。众所周知,他们已被弃用。请改用mysqli_*PDO