Forum Moderators: coopster

Message Too Old, No Replies

Storing query for later use

         

ofie2

6:13 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



Here's an easy one to solve.

I often use id's of other tables in my database to represent the information. For example:

Table: phone number types
id ¦ type
---------------
1 ¦ Home
2 ¦ Work
3 ¦ Fax

Table: people's phone
id ¦ person ¦ number ¦ type
-------------------------------
1 ¦ Sue ¦ 5551212 ¦ 2

On the people's phone table I assign a type id instead of the actual type. So everytime I want to list Sue's work number the table pulls the number then the number 2.

I want to create a function or an array so I only have to pull data from the phone number type table once per page, then simply compare the id fields to display the actual type.

Right now I query the people's table, then within each output I run a query to pull the id of the phone type and display the associated text. This is very wasteful.

Can you help me figure out a faster way? Perhaps a function or stored array.

Thanks,

Ofie

mooger35

8:41 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



How about just making 1 query?

ie:
$sql = "SELECT * FROM phone, type WHERE phone.id = type.id AND phone.id = '$id'";
$result = mysql_query($sql);

TheMadScientist

9:12 pm on Oct 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you help me figure out a faster way?

Can you outline your process a bit more... The most important question I can see needing the answer to is: Do you need to access the phone number on different pages or only one page? If you only need it on one page, then do as mooger35 suggested or something similar. Access the information once (at the top of the code) then use it throughout the code while you build the page. If you need to display it when a visitor is on Page A, then again on Page B and so on, then you have some options rather than making a query every time, but they're fairly limited.