This is one of those \'oh my god\' moments.
The previous programmer in our company has left behind a badly-coded application using PHP and MySQL.
One example
$a = '123,456,789';
$a_array = explode(",",trim($a));
if(in_array('456',$a_array))
{
echo 'exist';
} else echo 'not exist';
You could use FIND_IN_SET to retrieve rows that match your criteria:
SELECT * FROM your_table WHERE FIND_IN_SET('value', field_with_comma_sep_values) > 0;
Basically, FIND_IN_SET returns the index of the found item. So this query finds all rows where it finds the matching word in the "set" of comma separated values.
Credit: I knew there was something like this, but this post is where I found the answer and the SELECT statement.
So you're saying you want a query that returns all rows where a user has a specific option?
Well, ugh...okay, this isn't pretty, but you already know that, so:
select * from user_table
where concat(',',option_field,',') like "%,option_you_want,%"
I can't speak to the performance impact--it's likely not pleasant, but I think your other option is to break out the row into a separate table, but you're saying that's not realistically available to you.
What about creating a table that correctly breaks the CSV column into multiple columns (or multiple records) once as a batch process, and then cobbling together an update trigger on the old table to refresh the entry (or entries) for that user? Then you could write decent queries across it without having to re-code the setter logic.
Here's one idea for handling it without changing the schema or representation:
<?php
function checkoption ($user, $option)
{
global $db;
$rs = mysql_query ("select option_field_name from customer where user = '$user'", $db);
if ($rs)
{
// enclose result in commas to remove edge cases for next test
$opts = ",," . $rs ['option_field_name'] . ",";
return strpos ($opts, ",$option,") != false;
}
return false;
}
?>
It looks for ,option, in the field for the user, and guards against edge cases by handling all references with an extra pair of surrounding commas. To avoid the zero versus Null problem, an extra comma is added to the front of the db string.