searching for a part of a field in mySQL
Moderator: General Moderators
searching for a part of a field in mySQL
say for example I have a mySQL field that is a blog and i would like to find all blogs that contain that word how do I do it?
Thanks in advance
Thanks in advance
thank you for the help.
I dont really unberstand why the syntax
doesn't work?
:?
I dont really unberstand why the syntax
Code: Select all
"SELECT * FROM mainNewsItem WHERE item='*$search_keyword*':?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Because * isn't a wildcard in MySQL but % is and you would use LIKE not = for this sort of comparison so try:
Look at:
http://www.mysql.com/doc/en/String_comp ... ml#IDX1209
Mac
Code: Select all
SELECT * FROM mainNewsItem WHERE item LIKE '%$search_keyword%'http://www.mysql.com/doc/en/String_comp ... ml#IDX1209
Mac
thank, but for some reason that i cant work out this doesnt work in my script. consider
this works
albeit that only if you enter the whole field. Accoring to mySql.com this should work
but returns the 'error making query' error.
I bet its something simple I'm overlooking again but cant think what.
Thanks
Deej
?>
this works
Code: Select all
<?php
$query = "SELECT * FROM mainNewsItem WHERE item='$search_keyword' OR header='$search_keyword' OR short_text='$search_keyword' OR text='$search_keyword' OR author='$search_keyword' ORDER BY date DESC ";
?>Code: Select all
<?php
$query = "SELECT * FROM mainNewsItem LIKE item='%$search_keyword%' OR header='%$search_keyword%' OR short_text='%$search_keyword%' OR text='%$search_keyword%' OR author='%$search_keyword%' ORDER BY date DESC ";
?>I bet its something simple I'm overlooking again but cant think what.
Thanks
Deej
?>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You should be using LIKE instead of = as I stated before, and you also need a WHERE clause so instead of:
you should have something like:
Mac
Code: Select all
$query = "SELECT * FROM mainNewsItem LIKE item='%$search_keyword%' OR header='%$search_keyword%' OR short_text='%$search_keyword%' OR text='%$search_keyword%' OR author='%$search_keyword%' ORDER BY date DESC ";Code: Select all
$query = "SELECT * FROM mainNewsItem WHERE item LIKE '%$search_keyword%' OR header LIKE '%$search_keyword%' OR short_text LIKE '%$search_keyword%' OR text LIKE '%$search_keyword%' OR author LIKE '%$search_keyword%' ORDER BY date DESC ";