Web Design and Development Forums

Complicated database querys

This is a discussion on "Complicated database querys" within the MySQL section. This forum, and the thread "Complicated database querys are both part of the Databases category.


Go Back   Webforumz.com > Databases > MySQL

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Feb 1st, 2008, 23:03   #1 (permalink)
Junior Member
 
Join Date: Mar 2007
Location: Long Island, NY
Age: 22
Posts: 15
Send a message via AIM to linchpin311
Question Complicated database querys

greetings!

i am trying to query my database through php but its starting to get real complicated. this is the setup: a user visits page1.php to search for a name in the database. i want the user to be able to search for this name by user name, first name or last name. say they type in "John Smith" into the form...

the script then querys the database: SELECT * FROM users WHERE ((username LIKE '%John%') OR (firstname LIKE '%John%') OR (lastname LIKE '%John%')) OR ((username LIKE '%Smith%') OR (firstname LIKE '%Smith%') or (lastname LIKE '%Smith%'))

so im wondering is there an easier way to query the database and get the same results? I am planning to use another search for business names where users might type in more than two words and i would hate for the query to get any more complicated than it already is.

Many thanks in advance!
- Linchpin
linchpin311 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 2nd, 2008, 16:14   #2 (permalink)
Section Manager - WOTM
Assistant Editor - LZ
 
Jack Franklin's Avatar
 
Join Date: May 2007
Location: Cornwall, England
Posts: 1,101
Blog Entries: 5
Re: Complicated database querys

Surely though you will want to have the search query as a variable. EG:

PHP: Select all

$searchterm $_POST['search']; 

So you would simply search for the variable, but I have never done searches in MySQL. Some of the more PHP experts should be able to help you
__________________
Section Manager (WOTM)

My Weblog & E-Portfolio
Catch me daily on: Twitter | Digg | Flickr
Jack Franklin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 2nd, 2008, 16:23   #3 (permalink)
Junior Member
 
Join Date: Mar 2007
Location: Long Island, NY
Age: 22
Posts: 15
Send a message via AIM to linchpin311
Re: Complicated database querys

oh absolutely, im not that crazy...

i've done that...but i was hoping i could say something like:
Code: Select all
SELECT * FROM users WHERE (username OR firstname OR lastname) LIKE ('%John%' OR '%Smith%')
just to save some of my sanity. i have tried plugging that in instead of what i have now and it doesnt work properly. you think there is something similar i could say that would work and be not as long and tedious as what i have now?

Last edited by linchpin311; Feb 2nd, 2008 at 16:26.
linchpin311 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 3rd, 2008, 01:37   #4 (permalink)
 
Rakuli's Avatar
 
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 980
Blog Entries: 2
Re: Complicated database querys

To do the search that you want you had it right in the first instance. SQL is a simple language and doesn't really have many ways of shortening when you are having to look in multiple columns for differing values.

SQL ignores whitespace so if it is a matter of your sanity, break it up over numerous lines for easier reading. At the end of the day though, you really want to have it all in one query. Even if it is a very very long query, it is much faster than php.

If you're using PHP, here is how I would go about constructing the query so you don't have to worry about how long the search term is.

PHP: Select all



$search 
$_POST['search']; // Fill the variable with search

// This is the first part of the where clause searching for the whole string

$where " username LIKE %{$search}% OR firstname LIKE %{$search}% OR lastname LIKE %{$search}% ";

// Now we'll split that search term into an array by breaking it to pieces at spaces

$search explode(' '$search);

$nm $search// Count how many parts there are to the string

// If there was only one part then we won't get any more detailed than that

if ($nm 1)
{
    
// So there's more than one part so we'll add the individual searches to the where clause
    
foreach ($search as $s)
       
$where .= " username LIKE %{$s}% OR firstname LIKE %{$}% OR lastname LIKE %{$s}% ";

}

// Now the whole query has been constructed so it's just a matter of getting the results now

$result mysql_query("SELECT * FROM users WHERE $where");

// etc..... 
__________________
Rakuli

Helping you expand your epiphanies:

pen Source of pen Thoughts
Rakuli is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 3rd, 2008, 05:23   #5 (permalink)
Junior Member
 
Join Date: Mar 2007
Location: Long Island, NY
Age: 22
Posts: 15
Send a message via AIM to linchpin311
Re: Complicated database querys

wow, you must have hacked my desktop...thats pretty much exactly how i had choose to do it, granted it took me about 16 hours to figure out how to make it work this way...

good point about SQL and white space though, i wasn't thinking that way. i can break the query up over different lines and different variables. i kinda feel silly for not thinking about that before.
linchpin311 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 3rd, 2008, 10:54   #6 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Complicated database querys

Quote:
Originally Posted by Rakuli View Post
PHP: Select all


$search 
$_POST['search']; // Fill the variable with search

// This is the first part of the where clause searching for the whole string

$where " username LIKE %{$search}% OR firstname LIKE %{$search}% OR lastname LIKE %{$search}% ";

// Now we'll split that search term into an array by breaking it to pieces at spaces

$search explode(' '$search);

$nm $search// Count how many parts there are to the string

// If there was only one part then we won't get any more detailed than that

if ($nm 1)
{
    
// So there's more than one part so we'll add the individual searches to the where clause
    
foreach ($search as $s)
       
$where .= " username LIKE %{$s}% OR firstname LIKE %{$}% OR lastname LIKE %{$s}% ";

}

// Now the whole query has been constructed so it's just a matter of getting the results now

$result mysql_query("SELECT * FROM users WHERE $where");

// etc..... 
Not a bad bit of code, but you can do it all in one little line:
PHP: Select all

$where substr(preg_replace("(\w+)""OR (username LIKE '%$0%' OR firstname LIKE '%$0%' OR lastname LIKE '%$0%')"$_POST['search']), 3);

$result mysql_query("SELECT * FROM users WHERE $where"); 
Use a regular expression to
a) find each word
b) replace it with " OR username LIKE ..."
c) use the substr function to ignore the first "OR "

...and this would work much better as a function you can use in different applications. Write something once and re-use it 'til the cows come home'
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?

Last edited by spinal007; Feb 3rd, 2008 at 11:00.
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 3rd, 2008, 10:57   #7 (permalink)
 
Rakuli's Avatar
 
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 980
Blog Entries: 2
Re: Complicated database querys

Good point, didn't think to use a regex.
__________________
Rakuli

Helping you expand your epiphanies:

pen Source of pen Thoughts
Rakuli is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 3rd, 2008, 11:08   #8 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Complicated database querys

Here's that function (and a few demos):

PHP: Select all

function search_sql($words$sql){
 
$sql str_replace("WORD""$0"$sql);
 
$words substr(preg_replace("(\w+)","OR (" $sql ")"$words), 3);
    return 
$words;
};

print 
"Test 1: " search_sql('A B C'"username LIKE '%WORD%' OR firstname LIKE '%WORD%' OR lastname LIKE '%WORD%'") . '<br/>';
print 
"Test 2: " search_sql('X Y Z'"type='WORD'") . '<br/><br/>';
print 
"Test 3: " search_sql('John Doe'"firstname LIKE '%WORD%' OR surname LIKE '%WORD%'") . '<br/><br/>';
print 
"Test 4: " search_sql('John Doe'"surname LIKE '%WORD%'") . '<br/><br/>';
print 
"Test 5: " search_sql('UserX'"username='WORD' OR (firstname LIKE '%WORD%' OR surname LIKE '%WORD%')") . '<br/><br/>'
Test results

Test 1: (username LIKE '%A%' OR firstname LIKE '%A%' OR lastname LIKE '%A%') OR (username LIKE '%B%' OR firstname LIKE '%B%' OR lastname LIKE '%B%') OR (username LIKE '%C%' OR firstname LIKE '%C%' OR lastname LIKE '%C%')

Test 2: (type='X') OR (type='Y') OR (type='Z')

Test 3: (firstname LIKE '%John%' OR surname LIKE '%John%') OR (firstname LIKE '%Doe%' OR surname LIKE '%Doe%')

Test 4: (surname LIKE '%John%') OR (surname LIKE '%Doe%')

Test 5: (username='UserX' OR (firstname LIKE '%UserX%' OR surname LIKE '%UserX%'))
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
database Rasta MSSQL & Access 2 Aug 16th, 2007 12:07
XML Code for transfering data from one SQL Server Database to another database plolla XML, RSS & Atom 1 Aug 3rd, 2006 18:37
database help... BongMaster MSSQL & Access 1 Dec 6th, 2005 07:38



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 07:10.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59