I have the following function that searches a database:
public function search($table, $column, $term, $limit = 0, $offset = 0){
$command = "SELECT * FROM `$table`";
$size = 1; //1 is for when there is only one LIKE
if(is_array($column)){ //Creates the array statemenet
$size = sizeof($column);
for($i = 0; $i < $size; $i++){
if($i == 0){
$command.=" WHERE `$column[$i]` LIKE ?";
}else{
$command.=" AND `$column[$i]` LIKE ?";
}
}
}else{//If there's only one
$command.=" WHERE $column LIKE ?";
}
if($limit > 0){//Adds limits
$command.=" LIMIT ? OFFSET ?";
}
echo $command;
$query = $this->connection->prepare($command);
if(is_array($column)){
$x = 0;
foreach($term as $coldata){
$x++;
$type;
$coldata = "%{$coldata}%";
$query->bindParam($x, $coldata, PDO::PARAM_STR);
}
}else{
$term = "%{$term}%";
$query->bindParam(1, $term, PDO::PARAM_STR);
}
if($limit > 0){
$query->bindParam($size + 1, $limit, PDO::PARAM_INT);
$query->bindParam($size + 2, $offset, PDO::PARAM_INT);
}
$query->execute();
return ($query->fetchAll(PDO::FETCH_ASSOC));
}
and the $column and $term parameters of the function can be an array. If they are an array, the function will do a
WHERE x LIKE y AND z LIKE b
type thing.
However, when I test it, it does something like this:
Where x LIKE b AND z LIKE b
All of the LIKE parameters are the same. Here is the example data I provided:
$table="tableName"
$column=array('member','type')
$term=array('Bob','admin')
$limit=30
$offset=0
When I check my query logs, I get this:
SELECT * FROM `tableName` WHERE `member` LIKE '%admin%' AND `type` LIKE '%admin%' LIMIT 30 OFFSET 0
Why does it use admin on both of them and not Bob then Admin like so
SELECT * FROM `tableName` WHERE `member` LIKE '%Bob%' AND `type` LIKE '%admin%' LIMIT 30 OFFSET 0