简体   繁体   中英

How to get all matching records respectievelijk from MySql database using php which is stored in an array?

I have a MySql database within more then 1400 rows. Using of sql query I am getting matching rows and store them in an array. what I want to do is, all stored values show on my website.

File name is 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();
?> 

And i want to use this data on my website, I tried this code;

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

I am doing something wrong but what ? Because this give me just the last records value. If somebody can help me I will be very happy. Thank you.

First you need to wrap the OR condition in brackets in your sql

Then simply process the resultset in a while loop outputting the data from the query as you go.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM