I am trying to pull data from the database based on my search input. It works, however I receive only one row as a result, even if the query is sending an array. I think I have to put fetch in a while loop somehow.
<?php
$con = new PDO("mysql:host=localhost;dbname=va_final_project", 'root', '');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT gr.genre_name AS gr_name, band_name AS name_band
FROM `va_band_name` bn INNER JOIN va_genre gr ON bn.genre_id = gr.id
WHERE band_name = '$str' OR genre_name = '$str'");
$sth->setFetchMode(PDO::FETCH_OBJ);
$sth->execute();
if ($row = $sth->fetch()) {
while($row = $sth->fetch()) {
?>
<div class="separate">
<table id="bands_table_display" class="table table-dark">
<tr>
<th>Genre</th>
<th>Band</th>
</tr>
<tr>
<td><?php echo $row->gr_name; ?></td>
<td><?php echo $row->name_band; ?></td>
</tr>
</table>
</div>
<?php
}
} else {
echo "<h3 class='separate no-response'>Name Does not exist</h3>";
}
}
?>