我有一个超过 1400 行的 MySql 数据库。使用 sql 查询我得到匹配的行并将它们存储在数组中。我想要做的是,所有存储的值都显示在我的网站上。
文件名为filter.php
$sql = "SELECT * FROM champ where GG IS NULL OR GG= ' ' AND K=' ' AND J=' '";
$result = $conn->query($sql);
$array = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$array[] = $row;
}
for($i = 0, $j = count($array); $i < $j ; $i++){
$HomeTeam = $array[$i]['HOME'];
$AwayTeam = $array[$i]['AWAY'];
}
} else {
echo "0 results";
}
$conn->close();
?>
我想在我的网站上使用这些数据,我尝试了这段代码;
<table class="table">
<thead>
<tr>
<th>Home Team</th>
<th>Away Team</th>
</tr>
</thead>
<tr>
<?php
include 'filter.php';
echo"<td>". $HomeTeam."</td>";
echo"<td>".$AwayTeam."</td>";
echo "</tr>";
?>
</table>
我做错了什么,但是什么?因为这只给我最后的记录值。 如果有人能帮助我,我会很高兴。谢谢。
最佳答案
首先,您需要将 OR 条件括在 sql 中的括号中
然后只需在 while 循环中处理结果集,并随时输出查询中的数据。
$sql = "SELECT *
FROM champ
where (GG IS NULL OR GG= ' ')
AND K=' ' AND J=' '";
$result = $conn->query($sql);
$champs = array();
while ( $row = $result->fetch_assoc() ) {
$champs[] = $row;
}
?>
<table class="table">
<thead>
<tr>
<th>Home Team</th>
<th>Away Team</th>
</tr>
</thead>
<tbody>
<?php
foreach ($champs as $champ ) :
?>
<tr>
<td><?php echo $champ['HOME'];?></td>
<td><?php echo $champ['AWAY'];?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
关于php - 如何使用存储在数组中的php从MySql数据库获取所有匹配的记录respectievelijk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48875527/