I’m getting no errors, but also getting no results – mysql php db

I’m trying to grab a user submission from a database with two columns. One for Artist and one for title. I want to take their input from the simple form and output all of the similar results into a table on the next page. I’ve included the entire script I’ve written thus far. I’m not getting any errors on the page, but I’m also not getting any results. I’ve spent several days looking online to see if I can clear this up on my own, but I’ve had no such luck. Sorry for being so wordy, but I’m new to this site and wanted to provide as much detail as possible.

<?php 
include("db_connect.php"); 
// - get form data from "searchform.php"
$song_query = $_GET['song_query'];
// - column 1 and column 2 are all we're looking for in the db 
// - title and artist are currently the two cols. Table is named 
"song_list"
$col1 = "title";
$col2 = "artist";
$tablename = "song_list";
echo "<H1>Search results</H1>";
if ($song_query == "") {
echo "What are you going to sing? You didn't enter a search term! Please 
try again.";
exit;
}
// - pick the db connection out of the included file and give error if 
failed.
mysqli_select_db($conn, $db_name) or die(mysqli_error());
// - cleans up string inputted by user to avoid harmful code insertion 
into form
$song_query = strtoupper($song_query);
$song_query = strip_tags($song_query);
$song_query = trim($song_query);
// - set up parameters for accessing the query of the db
$sql = "SELECT $col1, $col2 FROM $tablename WHERE $col1, $col2 LIKE 
'%$song_query%'";
$result = mysqli_query($conn, $sql);
if (isset($_GET['$result'])){
if (mysqli_num_rows($result) > 0){
    echo "<table><tr>";
    echo "<th>Artist</th>";
    echo "</tr>";

while($row = mysqli_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row['$result'] . "</td>";
    echo "</tr>";
    echo "</table>";
    }
    }
}
?>

Answer

You have wrong SQL, which is getting constructed at runtime

$sql = "SELECT $col1, $col2 FROM $tablename WHERE $col1, $col2 LIKE 
'%$song_query%'";

which becomes

$sql = "SELECT title, artist FROM $tablename WHERE title, artist LIKE 
'%$song_query%'";

Look at WHERE title, artist LIKE here

$song_query gets value from $_GET['song_query'],which changes at runtime.

Leave a Reply

Your email address will not be published. Required fields are marked *