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
https://forums.phpfreaks.com/topic/269256-php-like-help/
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
https://forums.phpfreaks.com/topic/269256-php-like-help/#findComment-1383928
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
https://forums.phpfreaks.com/topic/269256-php-like-help/#findComment-1383929
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
https://forums.phpfreaks.com/topic/269256-php-like-help/#findComment-1383932
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
https://forums.phpfreaks.com/topic/269256-php-like-help/#findComment-1384003
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.