can anybody help me how I can use four tables at the same time with different conditions to fetch records from my database?
this is my sql code that im trying to modify because this is only for two tables:

<?php
session_start();
if (isset($_SESSION['username']))
{
include("database.php");
			$username = $_SESSION['username'];
			$query = "SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND p.username='$username'"; //this is the SQl code that Im trying to modify

		$result = mysql_query($query) or die(mysql_error());
		$patient = mysql_num_rows($result);
		if ($patient!=0)
		{
			while ($row = mysql_fetch_array($result))
			{
				$dMR_no = $row[0];
				$dusername = $row[1];
				$dpassword = $row[2];
				$demail = $row[3];
				$dlname = $row[5];
				$dfname = $row[6];
				$dgender = $row[11];
				$dmname = $row[7];
				$dage = $row[9];
				$dcontact_no = $row[12];
				$dlocation = $row[8];
			//	print_r($row);
			}
		}
		
		else
		{
			die(mysql_error());
		}
}
else
die("Please Login to view this page");
?>

this is the information of the tables that I want to use

tbllabchem: //this is the primary table
LabID_No	int(11)			No	None	auto_increment	 	 	 	 	 	 	 
 	RelationMR_No	int(11) 		UNSIGNED	No	None		 	 	 	 	 	 	 
 	RelationDoc_ID	int(11) 		UNSIGNED	No	None		 	 	 	 	 	 	 
 	Date	varchar(50)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Random_BloodSugar	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Glucose	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Bun	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Creatinine	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	bua	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Total_Cholesterol	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Triglyceride	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Hdl	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Ldl	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Vldl	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Oral_GlucoseTolerance	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Glucose_ChallengeTest	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Calciums	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Others	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Total_Protein	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Albumin	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Globulin	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	A/G_Ratio	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Sodium	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Potassium	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Chloride	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Total_Bilirubin	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Direct_Bilirubin	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Indirect_Bilirubin	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Sgot	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Sgpt	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Alk_Phos	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Med_Tech	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Pathologist

this is the tbldoctor_info where I want to get the name of the doctor base on the
RelationDoc_ID from tblbloodchem and ID_no from tbldoctor_info

ID_No	int(10) 		UNSIGNED	No	None	auto_increment	 	 	 	 	 	 	 
 	LastName	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	FirstName	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	MName	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Age	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Gender	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Address	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Specialist	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	License_No	int(10)

I also want to get the name of the patient from tblpatient_info based on the RelationMR_no from tblbloodchem and MR_no from tblpatient_info

tblpatient_info
MR_No	int(10) 		UNSIGNED	No	None	auto_increment	 	 	 	 	 	 	 
 	LastName	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	FirstName	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	MName	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Address	varchar(45)	latin1_swedish_ci		Yes	NULL		 	 	 	 	 	 	 
 	Age	int(10)			No	None		 	 	 	 	 	 	 
 	BP	int(10)			No	None		 	 	 	 	 	 	 
 	Gender	varchar(45)	latin1_swedish_ci		No	None		 	 	 	 	 	 	 
 	Contact_No	int(11)			No	None		 	 	 	 	 	 	 
 	Weight	int(10)			No	None		 	 	 	 	 	 	 
 	Temperature	varchar(45)	latin1_swedish_ci		No	None

I you where confused with my explanation, don't hesitate to ask me :)

Recommended Answers

All 2 Replies

Do not use PHP until you know how to code your MySQL queries.
List all tables which you need in the FROM clause and describe their relation in the WHERE condition - which fields have to match in one record?

select ... (desired fields here)
from tbllabchem, tbldoctor_info, tblpatient_info
where MR_No = RelationMR_No and ID_No = RelationDoc_ID

I got it,, sorry guys for disturbing you,, I already found the answer :)
this is my sql code

$username = $_SESSION['username'];
			$query = "select * from tblpatient_info i, tbllab_bloodchem b, tbldoctor_info d where i.MR_no = b.RelationMR_no and b.RelationDoc_ID = d.ID_no";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.