Jump to content

Php Like Help


mattm1712

Recommended Posts

hi i am trying to return values in my data base which are like what my varibale is

 

say my data base has these values is

 

1 Manchester Utd

2 Chester

3 Macclessfiled

4 Liverpool

 

 

and i have a variable $tl = "chester city";

 

and i wanted to echo out the values that look like 'chester city' eg i want a list that has chester in it

 

 

$tl = "chester city";

$query2 = mysql_query("SELECT * FROM teams LIKE '{$tl}'");

 

while($array2 = mysql_fetch_assoc($query2))

{

echo $array2['name'];

 

}

 

why whould this not work?

 

cheers matt

Link to comment
Share on other sites

I've moved this to MySQL.

 

The simple answer is that LIKE works using wildcards, and can only find matches where the search term is within the result.

So LIKE('%Chester%') would match 'Chester City', but LIKE('%Chester City%') will not match 'Chester'.

 

Link to comment
Share on other sites

LIKE is an operator that allows for wild cards. You can find anything LIKE '%a%', which is anything with a lowercase A in it. It's not a magical "read this and figure out what's similar" function. You have "chester city." Even if you split that into WHERE field LIKE '%chester%' AND field LIKE '%city%' you'd still get Man U in there.

 

Also, your query is fundamentally wrong, you can't just FROM table LIKE string

Link to comment
Share on other sites

hi i am trying to return values in my data base which are like what my varibale is

 

say my data base has these values is

 

1 Manchester Utd

2 Chester

3 Macclessfiled

4 Liverpool

 

 

and i have a variable $tl = "chester city";

 

and i wanted to echo out the values that look like 'chester city' eg i want a list that has chester in it

 

 

$tl = "chester city";

$query2 = mysql_query("SELECT * FROM teams LIKE '{$tl}'");

 

while($array2 = mysql_fetch_assoc($query2))

{

echo $array2['name'];

 

}

 

why whould this not work?

 

cheers matt

 

Now don't quote me on this because I'm a beginner but that's not going to echo anything because you're trying to call something which doesn't exist.

 

If in your DB you has "Chester City" and you queried something LIKE "Chester%", it would return "Chester City", but not the other way around.

 

I'm sure one of the experts on here will correct me if I'm wrong.

Link to comment
Share on other sites

Example

 

mysql> SELECT * FROM teams;
+----+-----------------------------+
| id | team_name				   |
+----+-----------------------------+
|  1 | History of Chester FA Clubs |
|  2 | Chester					 |
|  3 | Chester City			    |
|  4 | chester United			  |
|  5 | manchester united		   |
+----+-----------------------------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM teams
   -> WHERE team_name LIKE '%chester%';
+----+-----------------------------+
| id | team_name				   |
+----+-----------------------------+
|  1 | History of Chester FA Clubs |
|  2 | Chester					 |
|  3 | Chester City			    |
|  4 | chester United			  |
|  5 | manchester united		   |
+----+-----------------------------+
5 rows in set (0.00 sec)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.