mysql将两个不同的查询分成两个不同的html表

时间:2016-07-29 22:49:01

标签: php mysql

我想运行两个不同的mysql查询并将结果输出到两个不同的html表中。我正在打开一个数据库连接并获取两个完全不同的结果集。

我有一个页面,我想在页面中显示两个不同的页面,每个表格是来自不同查询的结果。

echo "<table class='table table-striped table-bordered table-hover table-condensed'>";                  
echo "<thead><tr>";
echo "<th>Last</th><th>First</th><th>MDC</th><th>RADIO</th><th>EPCR</th><th>FH</th>";
echo "</tr></thead></table>";   

while ($rowA = mysql_fetch_array($result)) {
    echo "<tbody><tr>";
    echo "<td>".$rowA['LAST']."</td>";
    echo "<td>".$rowA['FIRST']."</td>";
    echo "<td>".$rowA['MDC']."</td>";
    echo "<td>".$rowA['RADIO']."</td>";
    echo "<td>".$rowA['ePCR']."</td>";
    echo "<td>".$rowA['Firehouse']."</td>";
}
echo "</tr></tbody></table>";                       

echo "<table class='table table-striped table-bordered table-condensed'>";                  
echo "<thead><tr>";
echo "<th>USERNAME</th><th>CLASSNAME</th><th>DATE COMPLETED</th>";
echo "</tr></thead></table>";


while ($rowB = mysql_fetch_array($sql)) {
    echo "<tbody><tr>";
    echo "<td>".$rowB['UserName']."</td>";
    echo "<td>".$rowB['ClassName']."</td>";
    echo "<td>".$rowB['DateCompleted']."</td>";
}
echo "</tr></tbody></table>";

mysql_close($dbhandle);

这是我的查询:

$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");



$selected = mysql_select_db("tech_training",$dbhandle) 
or die("Could not select examples");


$result = mysql_query("SELECT LastName AS LAST, FirstName AS FIRST,
MAX(IF(`ClassName`='MDC (Intro)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'MDC', 
MAX(IF(`ClassName`='800 MHz Radio (Intro)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'RADIO',
MAX(IF(`ClassName`='ePCR (Intro)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'ePCR',
MAX(IF(`ClassName`='Firehouse (Incident)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'Firehouse'
FROM    EnrollmentsTbl INNER JOIN UsersDataTbl ON EnrollmentsTbl.UserName = UsersDataTbl.UserName
GROUP BY EnrollmentsTbl.UserName 
ORDER BY LastName
LIMIT 20;"); 


//execute the second SQL query and return records
$sql = mysql_query("SELECT UserName, ClassName, DateCompleted FROM EnrollmentsTbl LIMIT 10;");

1 个答案:

答案 0 :(得分:0)

在页面顶部的某处,或者最好使用include语句,因为根据PSR 1,你不应该将输出与函数/类混合。

使用 PDO ,这是一个例子。

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

$stmt = $db->query('SELECT * FROM table');
$rowA = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $db->query('SELECT * FROM table2');
$rowB = $stmt->fetchAll(PDO::FETCH_ASSOC);

<强> ANSWER

echo "<tbody>"; //this is outside
while ($rowB = mysql_fetch_array($sql)) {
    echo "<tr>";
    echo "<td>".$rowB['UserName']."</td>";
    echo "<td>".$rowB['ClassName']."</td>";
    echo "<td>".$rowB['DateCompleted']."</td>";
    echo "</tr>";// MOVE THIS INSIDE THE LOOP!!!

}
echo "</tbody></table>";

其他循环:

echo "<tbody>"; //outide
while ($rowA = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>".$rowA['LAST']."</td>";
    echo "<td>".$rowA['FIRST']."</td>";
    echo "<td>".$rowA['MDC']."</td>";
    echo "<td>".$rowA['RADIO']."</td>";
    echo "<td>".$rowA['ePCR']."</td>";
    echo "<td>".$rowA['Firehouse']."</td>";
    echo "</tr>"; //move end row here
}

echo "</tbody></table>";     

<强>说明

//you have some array
$result = [];

echo "<table>" // you only want one of these
foreach($results as $rows){
    echo "<tr>";
    echo "<td>data</td><td> mode data </td>";
    echo "</tr>"; close row each time
}
echo "</table>" //close it ONLY ONCE! 

<强>建议

正确和可读的格式化会为您节省。 EX:

  echo "<table class='table table-striped table-bordered table-hover table-condensed'>
      <thead>
          <tr>
             <th>Last</th><th>First</th><th>MDC</th><th>RADIO</th><th>EPCR</th><th>FH</th></tr>
      </thead>
  </table>";  

OR:

$table = <<<HTML
<table class='table table-striped table-bordered table-hover table-condensed'>
  <thead>
      <tr>
         <th>Last</th><th>First</th><th>MDC</th><th>RADIO</th><th>EPCR</th><th>FH</th></tr>
  </thead>
</table>
HTML;
echo $table;