how to use innerjoin to select records from two tables
Hello guys I am having a problem regarding queries in PHP because I not very familiar with queries especially inner join function, I need to make a profile page wherein it will display records from tblpatient_pass and tblpatient_info connected by (PK)MR_no from tblpatient_info and (FK)RelationMR_no from tblpatient_pass here is my sample code:
<?php
$query1 = "SELECT tblpatient_pass.RelationMR_no, tblpatient_pass.username, tblpatient_pass.password, tblpatient_pass.email_address, tblpatient_info.lastname, tblpatient_info.firstname, tblpatient_info.mname
FROM tblpatient_pass INNER JOIN tblpatient_info ON tblpatient_pass.RelationMR_no=tblpatient_info.MR_no
";
$numrows = mysql_query($query1);
if ($numrows!=0)
{
while ($row = mysql_fetch_array($query1))
{
$username = $_SESSION['username'];
$query = mysql_query("SELECT * FROM tblpatient_pass WHERE username =".$username);
$result = mysql_query($query) or die(mysql_error());
while ($patient = mysql_fetch_array($result))
{
echo $patient['RelationMR_no'].'<br>';
echo $patient['username'].'<br>';
echo $patient['password'].'<br>';
echo $patient['email_address'].'<br>';
echo $patient['lastname'].'<br>';
echo $patient['firstname'].'<br>';
echo $patient['mname'].'<br>';
echo $patient['gender'].'<br>';
echo $patient['age'].'<br>';
}
}
}
?>
Please tell me how can I select a record from two tables based on the username of the patient
Ctechnology24
Junior Poster in Training
63 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
SELECT *
FROM tblpatient_pass p, tblpatient_info i
where p.RelationMR_no = i.MR_no
and username='$username'
smantscheff
Nearly a Posting Virtuoso
1,297 posts since Oct 2010
Reputation Points: 321
Solved Threads: 270
Skill Endorsements: 8
thank you this helped me much :)
but still I get some error in my PHP project but I'll rather ask it in PHP forum,, thanks
Ctechnology24
Junior Poster in Training
63 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
I just came back here to say that your SQL codes worked, and also to say Thank you again! :)
Ctechnology24
Junior Poster in Training
63 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
smantscheff