This is a discussion on "Complicated database querys" within the Databases section. This forum, and the thread "Complicated database querys are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Complicated database querys
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
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 |
|
|
|
#2
|
||||
|
||||
|
Re: Complicated database querys
Surely though you will want to have the search query as a variable. EG:
__________________
Jack Franklin - Webforumz Moderator (x)HTML | CSS | PHP | MySQL | JQuery (Javascript) Contact: My Blog | Twitter | Delicious Want Lessons? PM me. If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
|
|
#3
|
|||
|
|||
|
Re: Complicated database querys
oh absolutely, im not that crazy...
i've done that...but i was hoping i could say something like:
Last edited by linchpin311; Feb 2nd, 2008 at 16:26. |
|
#4
|
||||
|
||||
|
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.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
Re: Complicated database querys
Quote:
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'
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Last edited by spinal007; Feb 3rd, 2008 at 11:00. |
|
#7
|
||||
|
||||
|
Re: Complicated database querys
Good point, didn't think to use a regex.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
#8
|
||||
|
||||
|
Re: Complicated database querys
Here's that function (and a few demos):
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%'))
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Database? | SpickenSpanDesign | PHP Forum | 18 | Dec 12th, 2006 19:07 |
| XML Code for transfering data from one SQL Server Database to another database | plolla | Other Programming Languages | 1 | Aug 3rd, 2006 18:37 |
| PHP in database | craig | PHP Forum | 12 | Dec 22nd, 2005 20:04 |
| database help... | BongMaster | Databases | 1 | Dec 6th, 2005 07:38 |