Forum Moderators: coopster

Message Too Old, No Replies

MySQL queries spanning multiple tables with different keys

         

LemonFizz

3:55 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



Hi,
I found this site whilst doing a search on complex joins across multiple tables in MySQL and was really impressed with the moderator's detailed and patient reply. ("no such thing as a nitwit around here ")
Well here's another nitwit question from a MySQL/PHP newbie.
I need to get information from an 11 table database but the particular query I am interested in only accesses five of those.
The tables are: (relevant Primary Key and Foreign Key fields only are shown)
USER
user_id PK
(also contains usersurname and userfirstname fields etc. etc.)

ANTIGEN
antigen_id PK
user_id FK

PROJECT
project_id PK
user_id FK

SUBPROJECT
subproject_id PK
project_id FK
mouse_id FK

MOUSE
mouse_id PK

As you may be able to guess I am in the field of healthcare research and am attempting to build a lab information management system.

The question I have (and i will use any answers as a template for my other SQL queries) is how to display the information from the tables so that those related results appear together. A plain english query would look something like this;
show the records of all the users and next to each users name display what antigens they own, and which antigens are associated with which mice.

The problem I am having is to try and link the tables together as the MOUSE table does not have the user_id as a foreign key but is linked to it through the SUBPROJECT.mouse_id then SUBPROJECT.project_id PROJECT.project_id and PROJECT.user_id finally back to USER.user_id

Any idea how I would perform these multiple joins?
Would I have to use subselects?

The PROJECT and SUBPROJECT tables are linked to other tables and I don't need to display any data from those tables,
Many thanks,
LF

coopster

4:14 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, LemonFizz!

I think I understand what you are looking for. Test this and see if it works for you:


$sql = "SELECT
USER.user_id,
ANTIGEN.antigen_id,
MOUSE.mouse_id
FROM USER
INNER JOIN ANTIGEN USING (user_id)
INNER JOIN PROJECT USING (user_id)
INNER JOIN SUBPROJECT ON (PROJECT.project_id = SUBPROJECT.project_id)
INNER JOIN MOUSE ON (SUBPROJECT.mouse_id = MOUSE.mouse_id)";

LemonFizz

4:25 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



That was unbelievably fast. Many thanks. I will try this and get back to you. Thanks once again.
LF

LemonFizz

11:45 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



Thanks again for the reply....I do have yet another question which displays my ignorance:
at the start you have written
$sql =
does this mean that you have assigned a variable to the sql "select" and to display it you have to use echo $sql?
sorry for the question but as I say I am very new to this.
Many thanks
LF
"to ask a question [and show your ignorance] is a moments shame. To not ask a question [and remain in ignorance forever] is a lifetime's shame"
Samurai maxim

coopster

1:31 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm sorry if I confused you. I made an assumption that you wanted to run the query from a PHP script. Yes, that is, in PHP, one way to assign a value to a variable [php.net]. You can run the SELECT statement from a command line if you wish without assigning it to a variable first.

If you were to use the PHP echo [php.net] function, it would only print out the value that was assigned to the variable, which in this case would simply echo the SELECT statement and not what you are probably thinking it would return, the actual data from the table in your database. To do that, you need to execute the query statement and read from the result set. The moderator has written a nice little tutorial in the PHP Forum Library that will erase some of the ignorance :)

Basics of extracting data from MySQL using PHP [webmasterworld.com]

Haitian Proverb: Ignorance doesn't kill you, but it does make you sweat a lot.

LemonFizz

2:51 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



Thanks a lot.
One other question...I'd like to reuse a lot of my code using "include", but keep my stuff secur(ish) by not keeping my scripts on the Document root of the webserver
I read in a php book the following:
"If you're not sure how to set up a folder outside of your Web root to be accessible only to the user PHP runs as (usually something like nobody on Unix systems or IUSER_computername on Windows), get in touch with your Web server's administrator for guidance."

Since I am the adminstrator and I don't know what I am doing could you tell me how to do this? From what I understand I have to edit the php.ini file include_path but I have to set permissions so that the path is readable by the user PHP runs as.

Questions:
How do I find out the user that PHP runs as? is this in httpd.conf?
How would one best go about storing scripts off the document root?
Many thanks....two hours of searching and I haven't come up with an answer yet...
LF

coopster

4:00 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>How do I find out the user that PHP runs as? is this in httpd.conf?

It seems as though you have installed PHP as an Apache module [php.net]. When PHP is used as an Apache module it inherits Apache's user permissions (typically those of the "nobody" user). You may want to read a bit more about Apache Security Tips for Server Configuration [httpd.apache.org].

>>How would one best go about storing scripts off the document root?

The following threads may provide enough to answer that question. If not, or if you want confirmation/assurance with your decision(s), by all means come back and ask!
Performance and Multiple Include User Functions [webmasterworld.com]
Accessing PHP includes folder in root dir from 2+ levels deep [webmasterworld.com]
php, templates and folder directives [webmasterworld.com]

LemonFizz

8:24 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



Thanks again.
I had a look in http.conf and the User is www

# User/Group: The name (or #number) of the user/group to run httpd as.
# . On SCO (ODT 3) use "User nouser" and "Group nogroup".
# . On HPUX you may not be able to use shared memory as nobody, and the
# suggested workaround is to create a user www and use that user.
# NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
# when the value of (unsigned)Group is above 60000;
# don't use Group #-1 on these systems!
#
User www
Group www
</IfModule>
</IfModule>

I also checked to see if PHP is being run as a module and it is
(right near the bottom of the file is


# PHP4 configuration
LoadModule php4_module modules/libphp4.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps)

see how out of my depth I am...this is pretty basic stuff I know.

Thanks for the thread links. I think what I would like to do is in one of them fro Nick_W:


I do this: Have a small file called ini.php in the root dir and call it on every page.

<?
## ini.php
ini_set('include_path', '/var/www/includes/');
?>

Then you only ever need do:

require('topnav.ihtml');


Are there any glaring security issues I should be worried about doing this?
Many thanks again
LF

LemonFizz

8:15 am on Jan 9, 2004 (gmt 0)

10+ Year Member



So I ran a little test.
I modified my php.ini with the following path

include_path = ".:/php/includes:/Library/Apache2/macfinc"

wrote a small script in the Apache document root called inctest.php which looked like this


<?php
include 'includetest.php';
?>


then one directory up from the doc root I made another little script in my new directory 'macfinc' where I will keep my include scripts called includetest.php


<html>
<body>
This is a test
</body>
</html>


and called inctest.php with my browser and it worked! :)
This is a) the first php I have written and b) it used an include statement which worked.
Can you tell me if security-wise this is OK....to use an absolute path in php.ini to my includes "library" which is one level up from the document root (htdocs) in Apache?
Many thanks,
LF

jatar_k

9:18 am on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It shouldn't pose any security issues that I know of.

Welcome to the boards LemonFizz and thanks for the kind words.

coopster

2:24 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Congratulations LemonFizz! And I concur with jatar_k, your setup looks fine to me.

Using ...an absolute path in php.ini to my includes "library" which is one level up from the document root (htdocs) in Apache... is a common practice. You see, it's a good thing to keep your includes library outside of the public realm (above the public/web root) as you have recognized. The reason you see the php ini_set function being used by Nick_W, myself and others is that sometimes we are dealing with shared servers in which we cannot modify the php.ini files -- the host wants to control that, and rightly so. The PHP developers recognized this issue and offered a solution via the ini_set function. Awfully nice of them, huh?

Once again, nice job and best regards -- coopster