using mysql "where like" in sql statement

  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 12th, 2011, 4:28 am

Kindly anyone let me know how, if possible to use something like:

Code: [ Select ]
select * from members where fullnames like %$q% or number like %$q%


in a MySQL select query.

I know I can use

Code: [ Select ]
select * form members where fullnames like %$q%


Am trying to use the 'where like' statement to query for a row and allow user to enter either fullnames or number as the search term.

Thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 12th, 2011, 4:28 am

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8926
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post June 12th, 2011, 10:07 am

I would think the above would work, its possible you are missing single quotes that may be needed as in:

SQL Code: [ Select ]
SELECT *
FROM members
WHERE fullnames LIKE '%$q%'
OR number LIKE '%$q%'
  1. SELECT *
  2. FROM members
  3. WHERE fullnames LIKE '%$q%'
  4. OR number LIKE '%$q%'


You can also use REGEX to do something similar if you have multiple values to search over:

SQL Code: [ Select ]
SELECT *
FROM members
WHERE fullnames REGEXP 'value1|value2|value3'
OR number REGEXP 'value1|value2|value3'
  1. SELECT *
  2. FROM members
  3. WHERE fullnames REGEXP 'value1|value2|value3'
  4. OR number REGEXP 'value1|value2|value3'
Ozzu Hosting - Want your website on a fast server like Ozzu?

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 179 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.