Edit: Thanks to spencer7593 for helping me solve this. I have edited the main code to include the correct form for those who come later.
I am currently wrapping my head around PHP and mySQL and have run into a roadblock on searching mySQL.
I need to search multiple columns in a table for partial matches. I have got this working properly with a fixed set of columns to search, but when I try and use an array it fails, even though the output to mySQL seems identical.
Can anyone point out the tiny silly error I have made somewhere? That, or an improved way of doing it?
<form name="search" method="post" action="<?php $PHP_SELF; ?>">
Search for: <input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php
$dbdata=array(
'servername' => "localhost",
'username' => "Username",
'password' => "Password",
'dbname' => "Membership",
'table_Members' => "Members"
);
$fields=array(
'MemberID',
'FirstName',
'LastName',
'Nickname',
'Interests',
'Skills');
$output=array();
if( isset($_POST['searching']) ) {
$output=read_db_search($dbdata['table_Members'],$_POST['find'],$fields);
}
echo ('<pre>');
var_dump($output);
echo ('</pre>');
function read_db_search ($table,$searchQuery,$fields)
{
global $dbdata;
$servername=$dbdata['servername'];
$dbname=$dbdata['dbname'];
$dbtable=$dbdata['table_Members'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbdata['username'], $dbdata['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*This version works!!!!*/
$query = "SELECT * FROM `Members` WHERE `"
. implode("` LIKE '%$searchQuery%' OR `",$fields)
. "` LIKE '%$searchQuery%' " ;
/* This section should work but doesnt */
//$query="SELECT * FROM `Members` WHERE`'.implode(' ` LIKE \'%$searchQuery%\' OR `',$fields).'` LIKE \'%$searchQuery%\'";
//$stmt = $conn->prepare($query);
/* This one works fine */
/* $stmt = $conn->prepare("SELECT * FROM `Members` WHERE
`MemberID` LIKE '%$searchQuery%' OR
`FirstName` LIKE '%$searchQuery%' OR
`LastName` LIKE '%$searchQuery%' OR
`Nickname` LIKE '%$searchQuery%' OR
`Interests` LIKE '%$searchQuery%' OR
`Skills` LIKE '%$searchQuery%'");
*/
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$data=$stmt->fetchAll();
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
return $data;
}
?>
A var_dump of $query returns:
string(89) "SELECT * FROM `Members` WHERE`'.implode(' ` LIKE \'%searchterm%\' OR `',Array).'` LIKE \'%searchterm%\'"
with an error of:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'%searchterm%\' OR `',Array).'` LIKE \'%searchterm%\'' at line 1
$query, and include that in your question as well..implode(needs to be outside of the double quotes... put a double quote before that dot, (to terminate the preceding string literal). And you'll need another double quote after the).at the end of the implode(). And it looks like "Array" needs to be replaced with a reference to$fields. This isn't really a MySQL problem, this is a problem with PHP string handling. (The contents of$queryneed to be valid SQL text.)