php 和 MYSQL 新手,需要一些帮助,所以请温柔一点 我有一些使用 php echo 填充 HTML 表的代码。 声明是:
$query= "SELECT Distinct AgeGrp, Gender,Dist, Stroke, First,
Family, Name, Year, Time, Event, Date
FROM JLSSWIMMING1
WHERE Year = '7'
and Stroke = 'Freestyle'" ;
我想添加更多选择语句,然后在同一个 html 表中显示结果,例如
$query2= "SELECT Distinct AgeGrp, Gender,Dist, Stroke, First,
Family, Name, Year, Time, Event, Date
FROM JLSSWIMMING1
WHERE Year = '7'
and Stroke = 'Backstroke'" ;
提前致谢
从研究来看,我可能需要一个数组(而不是连接),但我目前正在努力。 这是我当前的代码:
<?php
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query= "SELECT Distinct AgeGrp, Gender,Dist, Stroke, First,
Family, Name, Year, Time, Event, Date
FROM JLSSWIMMING1
WHERE Year = '7'
and Stroke = 'Freestyle'" ;
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 0) {
echo "<br>";
echo '<table class="special-table">';
echo '<thead class="fixed-header">';
echo '<tr>
<th>Pos.</th>
<th class="left-align">Name</th>
<th class="left-align">Stroke</th>
<th>Yr</th>
<th>Dist</th>
<th>Time</th>
<th>AgeGrp</th>
<th>Gen.</th>
<th>DAte</th>
<th>Event</th>
</tr></thead><tbody>';
foreach ($result as $row) {
echo "<tr>
<td >" . $cnt++ ."</td>
<td class='left-align'>" . $row["Name"] . "</td>
<td class='left-align'>" . $row["Stroke"] . "</td>
<td >" . $row["Year"] . "</td>
<td >" . $row["Dist"] . "</td>
<td >" . $row["Time"] . "</td>
<td >" . $row["AgeGrp"] . "</td>
<td >" . $row["Gender"] . "</td>
<td >" . Date('d-m-y', strtotime($row['Date'])) . "</td>
<td >" . $row["Event"] . "</td>
</tr>";
}
echo "</table></div>";
} else {
echo "<br>";
echo "<br>";
echo "<div style='font-size: 42px;'> No results found ";
}
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
// Close the database connection
$pdo = null;
?>
使用这样的一个查询即可在一次选择中获取您想要的所有数据。您应该始终尝试运用查询的力量,而不是将关系数据库视为平面文件。
$query= "SELECT Distinct AgeGrp, Gender,Dist, Stroke, First,
Family, Name, Year, Time, Event, Date
FROM JLSSWIMMING1
WHERE Year = '7'
and ( Stroke = 'Freestyle' OR Stroke = 'Backstroke')";
或者你可以像这样使用 IN() 子句
$query= "SELECT Distinct AgeGrp, Gender,Dist, Stroke, First,
Family, Name, Year, Time, Event, Date
FROM JLSSWIMMING1
WHERE Year = '7'
and Stroke IN('Freestyle', 'Backstroke')";