I've scoured the internet, yet I couldn't find something like my problem. I'm trying to build a live search function that can pull data from multiple tables. It works with one table, but I want to be able to pull from bands, users, (albums, etc.)
Here's my PHP code:
require ("includes/config/config.php");
$search_term = sanitize(htmlentities($_POST['search_term']));
if (!empty($search_term)){
$search = "(SELECT `band_id`, `band_name` FROM `bands` WHERE `band_name` LIKE '%$search_term%' LIMIT 0, 5)
UNION ALL
(SELECT `user_id`, `username` FROM `users` WHERE `username` LIKE '%$search_term%' LIMIT 0, 5)";
$query = mysqli_query($conn, $search);
$result = mysqli_num_rows($query);
while ($row = mysqli_fetch_assoc($query)){
#$user_id = $row['user_id'];
#$username = $row['username'];
$band_id = $row['band_id'];
$band_name = $row['band_name'];
$check = mysqli_num_rows($query);
if ($check != 0){
echo "<a style='text-decoration: none; color: black;' href='index.php?band=$band_id'><li class='searchResults'>" . ucfirst($band_name) . "</li></a>";
} else {
echo "<li class='searchResults'>No Results Found</li>";
}
}
}
jQuery:
$("#searchbar").keyup(function(){
var searchTerm = $(this).val();
$.post('search.php', { search_term: searchTerm}, function(data){
$(".searchResults").html(data);
$("#searchUl").css("display", "block");
});
});
Again, everything else runs fine, but I don't know how to query multiple tables in a way so I can echo out the href differently (i.e. index.php?band=$band_id for band and index.php?user=$user_id for user.