I'm a database noob, how do I search for rows by matching items in a PHP array with items in a MySQL imploded array? It's stored like that out of convenience, but if there's a better way to do what I want, I'm all ears. I'm looking for something like this (I use ** to indicate where I'm handwaving):
$sql = "SELECT * FROM codeunits";
$sql .= " WHERE CASE WHEN private=1 THEN creatorID=:userID ELSE true END";
if (isset($tags))
{
$tag_ids = join(',',$tags);
$sql .= " AND ****items in mysql array 'tags' IN :tags";
if($stmt = $this->_db->prepare($sql)) {
$stmt->bindParam(":userID", $userID, PDO::PARAM_STR);
$stmt->bindParam(":tags", $tag_ids, PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
}
}
Here is how I am storing off the items into the DB. $values is bound to the query for the tags column, thus why I'm calling it a MySQL array.
$tags = array();
foreach ($_POST['tags'] as $selectedOption)
{
$tags[] = $selectedOption;
}
$values = implode(",",array_values($tags));
$sql = "INSERT INTO "
."codeunits(tags)"
." VALUES(:tags)";
if($stmt = $this->_db->prepare($sql)) {
$stmt->bindParam(":tags", $values, PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
return true;
}
This results in an entry in my db that looks like 'tag1,tag2,tag3'. Very simple string.