gpt4 book ai didi

php - mysql 两个不同的查询到两个不同的html表

转载 作者:行者123 更新时间:2023-11-29 20:39:51 24 4
gpt4 key购买 nike

我想运行两个不同的 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;");

最佳答案

在页面顶部的某个位置,或者最好使用 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);

回答

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!

推荐

正确且可读的格式可以拯救你。例如:

  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>";

或者:

$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;

关于php - mysql 两个不同的查询到两个不同的html表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38668540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com