I'm trying to implement a pretty simple search form to my homepage, but it does not completely work as I want it to..
It looks like the link from the form works good enough, and even the database search is working now, thanks to the latest updates.
The problem is: When the search is ready and the results are displayed (echo $output), I always see only one result, but I want all matches to be displayed..
Any suggestions?
Thanks in advance :)
Here comes the search.php file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
$conn = mysqli_connect("localhost", "user", "password", "table");
if(mysqli_connect_errno() ) {
echo "Failed to connect: " . mysqli_connect_error();
}
?>
<?php
$output = '';
if(isset($_GET['query']) && $_GET['query'] !== ' ') {
$searchquery = $_GET['query'];
$query = mysqli_query($conn, "SELECT * FROM tbl_users WHERE username LIKE '%$searchquery%' OR email LIKE '%searchquery%'") or die(mysqli_error());
$num_rows = mysqli_num_rows($query);
if($num_rows== 0) {
$output = 'No search results for <b>"' . $searchquery . '"</b>';
} else {
while($row = mysqli_fetch_array($query)){
$id = $row['id'];
$username = $row['username'];
$email = $row['email'];
$output = "<a href='profile.php?user=$id'>$username</a>";
}
}
} else {
header("location: ./");
}
print("$output");
mysqli_close($conn);
?>