How do I query a database with php using bind variables?

Joshua Maza

I'm making a song database system, and I'm trying to implement a way for users to search songs based on a category to search (song's name, artist's name, album name, genre etc) and a given search term. To accommodate user input and protect against SQL injection, I'm using a prepared statement made with bind variables, however I'm having trouble with what I currently made:

   search("genre", "electropop", $db);
   function search($column, $term, $db) {
     try {
      $sql = "SELECT * FROM Songs WHERE :column=:term;";
      $stmt = $db->prepare($sql);
      $params = array("column" => $column,
                      "term" => $term);
      $stmt->execute($params);
      $arr = $stmt->fetchALL(PDO::FETCH_ASSOC);
      print (json_encode($arr));
    }
    catch(PDOException $ex) {
      catch_error("The search failed.", $ex);
    }
  }

Whenever I test this, I get an empty array back: [ ] I tested my query ("SELECT * FROM Songs WHERE genre='electropop'") in phpmyadmin and it checks out (gave me back entries). The correct syntax for WHERE clauses in SQL is that the term needs to be surrounded by quotations (https://www.w3schools.com/sql/sql_where.asp) so I tried escaping quotation marks around the term:

  ...
  $sql = "SELECT * FROM Songs WHERE :column=\':term;\'";
  ...

But then it fails to even see :term as a token to bind variables to later.

Not sure how to go about solving this. I assumed the empty array is due to a valid search but just no results, but perhaps I'm not correctly using a prepared statement. Any help would be much appreciated! Thanks!

MH2K9

You missed the : before term param. Don't need to bind column name. Just use the $column var instead of :column.

search("genre", "electropop", $db);
function search($column, $term, $db) {
try {
    $sql = "SELECT * FROM Songs WHERE $column=:term;";
    $stmt = $db->prepare($sql);
    $params = array(":term" => $term);
    $stmt->execute($params);
    $arr = $stmt->fetchALL(PDO::FETCH_ASSOC);
    print (json_encode($arr));
    }
    catch(PDOException $ex) {
        catch_error("The search failed.", $ex);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I randomly get variables from a mysql database? (using php with dbo connection)

How do I return the result of a database query through a php variable to another page using the include function

How can I do an OR query for the same database column using ParseQuery?

How to update database using PHP variables?

How can I do this as 1 database query?

How do I query a relation database in Laravel?

In Flask how do I query my database using SQLAlchemy so that I filter for multiple things?

How do I query the database using Mongodb after I set up a Mongoose schema for authentication?

How do I bind data from MySQL database to Xamarin ListView?

How do i bind a react component to the database state?

How do I select data from an sqlite3 database into variables using sqlite3_exec?

using variables in $_POST to Query Database

How do I do a sql query inside the .hbs files like we do a for .html using php?

How do I query a database in PHP and return results based on matching user-input?

How do I issue a SQL query using pyodbc that involves python variables as the parameters?

How can I do this with variables in php?

How do I pass JavaScript variables to PHP?

How do I pass variables to PHP on sort?

How do i work with dynamic variables in PHP

How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

How do I produce nested JSON from database query with joins? Using Python / SQLAlchemy

How do I query a Data from a MySQL database using Tkinter Entry

How do I print a list from a Firebase Database query using Flutter?

How do i query mysql database using where like or like in array?

React Native Firestore: How do I listen for database changes at the same time as using the .where() query?

How do I remove the query string in PHP?

How do I run an sql query in php?

How do I bind a closure to a static class variable in PHP?

How to convert to Materialized view query with bind variables