Jump to content

Mysql + Arrays


lordfirex

Recommended Posts

I have a problem that i'm not quite sure how to do.

 

I have to create a system that allows me to enter several postcodes associated with a particular event. (These are 4 digit  postcodes).  Somebody then has to enter a Postcode and it should pull up all events associated with that postcode. The problem being that each event (For i can't see another way of storing it easily) will have the postcodes stored in a mysql database in an array. How do i get SELECT to search through the postcode arrays and then print out any rows that have that particular postcode in it.

Link to comment
Share on other sites

I have come to the conclusion to use relationships to store the postcodes. Although now i need to figure out how to do a table-wide search of all columns.

 

What i have now is

$result = mysql_query("SELECT * FROM postcode WHERE *='3912'");

 

Where the * is a wildcard, although of course it doesn't work :(

 

So i am completely lost :(

 

I can't think of where to go from there.

Link to comment
Share on other sites

MySQL is relational, so you need a relational table, in fact, a many to many.

 

So, you would have a table with id and postcode.

And, a table with postcode_id and event_id that would tie both of these tables together.

 

This way postcode and event is stored once, and then a list of the relationships between the events and postcodes is stored.  Which is easily sorted.

 

SELECT event.* FROM event  WHERE  event.id IN (SELECT event_id FROM events_postcodes JOIN postcodes ON events_postcodes.postcode_id = postcodes.id WHERE postcodes.postcode = '5432')

 

I think that is close, un-tested though.

Link to comment
Share on other sites

To clarify, these are the extra two tables.

CREATE TABLE `postcodes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `postcode` int(4) NOT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `events_postcodes` (
  `event_id` int(11) NOT NULL,
  `postcode_id` int(11) NOT NULL
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.