0

I have a social networking site which I am in the process of constructing. Users can follow each other and it adds their name to a list of followers. I need to scan that list for the session username cookie to choose which statuses (posts) to display. In my sql recordset, I have the code:

SELECT *
FROM statuses
WHERE followedby CONTAINS '". $_SESSION['MM_Username']."' 

But this just brings up error

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 'CONTAINS 'Usernamecookie'' at line 1

Harvey Fletcher
  • 1,167
  • 1
  • 9
  • 22

4 Answers4

2

Try this:

SELECT *
FROM statuses
WHERE followedby LIKE '%". $_SESSION['MM_Username']."%' 

LIKE clause is used for string matching.

Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38
1

Others have correctly answered this question already, but I just want to add the meaning of contains in MySQL:

MySQL only recognizes the CONTAINS SQL function when dealing with spatial data. It requires two graphics objects as arguments, and returns a 1 or 0 depending on if the first object completely contains the second. Designed as an implementation of the OpenGIS framework, the MySQL CONTAINS function does not work on ordinary strings, and will produce an error if you try it. MySQL only recognizes the LIKE and STRCMP functions when dealing with string of information.

So in your case, you want to use like.

References:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Nadine
  • 1,620
  • 2
  • 15
  • 27
0

Try this:

mysql_query("
SELECT *
FROM statuses
WHERE followedby LIKE '%{$MM_Username}%' 
");

Put the MM_Username in its own var. Nicer code ;) Remember to enable index for FollowedBy if your table grows.

Read more about how to enable fulltext indexes here

Community
  • 1
  • 1
MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
0

Instead of Contains use LIKE.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49