In a table I have a field that stores users who are allowed to see a post. I have something like 93849038,39480398 where the commas separates the users.
So this is what I am trying to do:
$user = logged in user; example: 93849038.
And this is what my query looks like:
$sql = "SELECT note FROM notes_are WHERE user_allowed = '$user' ORDER
BY id DESC LIMIT 0, 3";
Using a structure like that I am unable to serve the post.
This is not a recommended practice as it forces the database engine to inspect all rows of your table. It’s better to create a permission table where each user that has access to a note are listed separately (make sure to also index the user column).
If you really REALLY have to, you can use the
LOCATEstring function (assuming MySQL): http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_locateOf course,
LOCATEhas issues too because it doesn’t care about comma delimited stuff, so the “better” approach might beREGEXP: http://dev.mysql.com/doc/refman/5.5/en/regexp.html#operator_regexp