I am trying to create a small script that allows to me to search a database of song data.
Basically, I have a database with 3 tables, song title, artist name, and album. For all intents and purposes, album can be ignored, it’s just there for the moment.
What I need is to be able to pull the results from both fields (artist, and title). For example, if I enter “Tom American”, it should bring up Tom Petty - American Girl. However I do not get that at all. I get a blank result (might be something else, so I’m including the entire script). I’m also not getting a “No results found” message on this script either. Any help would be greatly appreciated. Please feel free to correct anything else I did wrong
[php]<?php
/*
This file controls the search results for the mobile application and the mobile website.
Do not edit this file without permission from the developer, or unless you know what you are doing!
*/
/* Display Errors (enable when there is an issue ONLY) */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(E_ALL);
// $attrs is optional, this demonstrates using persistent connections,
// the equivalent of mysql_pconnect
$attrs = array(PDO::ATTR_PERSISTENT => true);
// set query as blank
$final_query = “”;
// if user searches artist only
if (isset($_GET[‘Artist’])) {
$query_text = $_GET[‘Artist’];
$query_type = “Artist”;
$final_query = “WHERE artist LIKE ‘%$query_text%’”;
//echo $final_query;
}
// if user searches title only
if (isset($_GET[‘Title’])) {
$query_text = $_GET[‘Title’];
$query_type = “Title”;
$final_query = “WHERE title LIKE ‘%$query_text%’”;
//echo $final_query;
}
// If user searches all
if (isset($_GET[‘All’])) {
$query_text = $_GET[‘All’];
$query_type = “All”;
$final_query = “WHERE artist LIKE ‘%$query_text%’ AND title LIKE ‘%$query_text%’”;
//echo $final_query;
}
// connect to PDO
$pdo = new PDO(“mysql:host=localhost;dbname=XXXXXXXXXXXX”, “XXXXXXXXXX”, “XXXXXXXX”, $attrs);
$pdo->exec(“set names utf8”);
// the following tells PDO we want it to throw Exceptions for every error.
// this is far more useful than the default mode of throwing php errors
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare the statement. the place holders allow PDO to handle substituting
// the values, which also prevents SQL injection
$stmt = $pdo->prepare(“SELECT * FROM tblsongdb “.$final_query);
echo “
// set PDO to null in order to close the connection
$pdo = null;
?>[/php]