Current location: Hot Scripts Forums » Programming Languages » PHP » MYSQL Design Help


MYSQL Design Help

Reply
  #1 (permalink)  
Old 04-16-06, 03:20 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Angry MYSQL Design Help

Hi forum

I got problems. Ive already setup the database. I didnt go through any normalisation as I thought that since the data was so unique that slitting it into more than one table would just give me problems. I am now faced with query problems as running a query is quite complex. I have 20 fields. I have offered the visitor a search page which searches 7 of the 20. My first problem was the title search which explodes the words and sends them to the query string. I then need to add the other 6 search variables. Some are static like type which you can only select 1 from the list (or none for anything). My query string ends up being;

SELECT * FROM `events` WHERE title LIKE '%$value%' OR title LIKE '%$value%' OR title LIKE '%$value%' OR title LIKE '%$value%' OR title LIKE '%$value%' OR venue LIKE '%$value%' OR venue LIKE '%$value%' OR venue LIKE '%$value%' OR area LIKE '%$value%' OR area LIKE '%$value%' OR area LIKE '%$value%' OR area LIKE '%$value%' OR date = '$value' OR organiser LIKE '%$value%' OR organiser LIKE '%$value%' OR type = '$value' OR admission = '$value'

This is quite stupid and hard to work with. In an ideal world I would have just a name search but with dates and venues and such like envloved I need to narrow down the results.

After re-designing my tables (on paper) through a normalisation process I find that the query is going to be the same except I now would need to join the tables together in a query.

I guess my question is, how do I perform individual queries and join them effectivley so that I can use MYSQL's sort by functions. Ive already tried to write the results to a php 2dim array which has resulted in me losing more hair. I then couldnt get the commands to sort arrays out to work on a 2dim array. they seem to only want to work on a 1dim array.

Ive searched the web looked at my books and find Im really stuck. Can anyone please help.

Thanks
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Reply With Quote
  #2 (permalink)  
Old 04-16-06, 04:18 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Question

Ok - I think Im just confusing everyone including myself.

I guess its normal to have OR this OR that all over the place when you query MYSQL. So Ill just go with the flow. Just seemed that its BAD programming but then again this is the first time Ive delt with a relational db, Ive always just used arrays in my past experiences with other off line programming.

anyway I bunched all my queries together into one and ran it. Im a bit confused as to what to make of it.

example table

name --------- sex
---------------------
jack a ---------- man
jill z ------------ woman
jenny e --------- woman
---------------------

WHERE 'name' LIKE '%jack%' OR 'name' LIKE '%jill%' OR 'name' LIKE '%jenny%' AND 'sex' = 'man'

you should end up with
--------------------
jack a ---------- man
--------------------
because of the AND =

except I get
---------------------
jack a ---------- man
jill z ------------ woman
jenny e --------- woman
---------------------

then I noticed that I havent been putting --> ' <-single quotes around my field titles. Ive played around with this and can't find why the results change like they do. Can someone explain the difference.

WHERE name LIKE '%jack%' OR name LIKE '%jill%' OR name LIKE '%jenny%' AND sex = 'man'

WHERE 'name' LIKE '%jack%' OR 'name' LIKE '%jill%' OR 'name' LIKE '%jenny%' AND 'sex' = 'man'
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."

Last edited by DAL; 04-16-06 at 04:22 AM.
Reply With Quote
  #3 (permalink)  
Old 04-16-06, 11:11 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
The AND operator has higher precedence than the OR operator. Therefore, it will be evaluated before the OR's will. To force your logic to do what you expect, enclose the terms in ( ) -
Code:
WHERE ('name' LIKE '%jack%' OR 'name' LIKE '%jill%' OR 'name' LIKE '%jenny%') AND 'sex' = 'man'
As to the quotes, table names and field/column names should not have quotes. Strings should have quotes. In your examples, 'name' LIKE ... should probably not be returning any thing, but it is acting like the WHERE clause is always true. In this example, 'name' is literally the string name and not the column name. The above should be -
Code:
WHERE (name LIKE '%jack%' OR name LIKE '%jill%' OR name LIKE '%jenny%') AND sex = 'man'
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #4 (permalink)  
Old 04-16-06, 11:35 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
For your title search/complex query, you might take a look at the full text search functions - http://dev.mysql.com/doc/refman/5.0/...xt-search.html
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Useful MySQL Resources ptesone Database 30 02-23-11 08:28 AM
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
MySql Labyrinth moron Database 4 02-22-08 02:46 AM
UPDATE: MySQL Auto Backup & Export v1.1 Beyonder General Advertisements 2 03-21-05 02:05 PM
C/C++ - 2D/3D Designers - PHP Mysql Web Design Required KingsZone Job Offers & Assistance 2 09-13-04 03:00 PM


All times are GMT -5. The time now is 07:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.