taz321 Posted January 29, 2008 Share Posted January 29, 2008 Hi I have two tables Employee Form The foreign key of the form is EmployeeID. Now heres the problem. I want to display the EmployeeID in my form but as you know if i did then the ID number of the Employee would display instead of the Full Name (which is what i want). e.g The employeeID of Tom is 2. In my form i want to display the name Tom by using an SQL query which recognises the ID and returns the name of that ID. I have had a start but its wrong. <?php require "connect.php"; $query1 = "select * from form WHERE empName = surname"; $result1 = mysql_query($query1, $connection) or die ("MySQL Error: ".mysql_error()); ?> empName (displays the primary key of the employee table) surname (i want it to display the surname of the employee by recognising the empName) Any help would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/ Share on other sites More sharing options...
p2grace Posted January 29, 2008 Share Posted January 29, 2008 Basically you'd have to use a simple join. $query = "SELECT e.surName FROM `Employee` e, `Form` f WHERE e.empName = f.empName"; $run = mysql_query($query); This is assuming that surName is the actual name of the employee, and empName is the id of the employee. Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452405 Share on other sites More sharing options...
craygo Posted January 29, 2008 Share Posted January 29, 2008 Try this I am not sure what the key field is in the employee table, but in this example I will assume it is the same as in the form table, employeeID $query1 = "SELECT * FROM form JOIN employee ON form.employeeID=employee.employeeID WHERE empName = 'surname'"; if you give me the structure of both tables I could help out a little more. Ray Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452409 Share on other sites More sharing options...
taz321 Posted January 29, 2008 Author Share Posted January 29, 2008 This is the 'employee' Table employeeID title firstname surname username password active This is the 'form' Table formID issuetitle datesubmitted timesubmitted systemaffected prioritylevel issuedetails supportcomments clientFname clientSurname teamname empName clientID In the form table, the empName displays the employeeID from the employee table, hope this helps. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452417 Share on other sites More sharing options...
craygo Posted January 29, 2008 Share Posted January 29, 2008 what field in the form table references the employeeid in the employee table?? Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452420 Share on other sites More sharing options...
craygo Posted January 29, 2008 Share Posted January 29, 2008 Sorry I was editing the last post when time ran out so disregard. I replaced the surname with xxx, because empname is a reference to the employee id which is an interger. the empname should be, in the case above, 2. The xxx should be some value passed from the web page. <?php $query1 = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'xxxx'"; $res = mysql_query($query1) or die(mysql_error()); while($rows = mysql_fetch_assoc($res)){ echo $rows['firstname']." ".$rows['lastname']."<br>"; } ?> That should give you your first and last name based off toms employeeid of 2 Ray Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452424 Share on other sites More sharing options...
taz321 Posted January 29, 2008 Author Share Posted January 29, 2008 So this is the statement im now using <?php $query1 = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'"; $result = mysql_query($query1) or die(mysql_error()); while($rows = mysql_fetch_assoc($result)){; echo $rows['surname']; } ?> And then i echo it <?php echo $row['surname'];?> but it doesnt seem to work, any reason why ? Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452435 Share on other sites More sharing options...
taz321 Posted January 29, 2008 Author Share Posted January 29, 2008 can anyone help me with this please. Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452856 Share on other sites More sharing options...
themistral Posted January 29, 2008 Share Posted January 29, 2008 Try changing this line while($rows = mysql_fetch_assoc($result)){; to while($rows = mysql_fetch_array($result)){ Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452882 Share on other sites More sharing options...
taz321 Posted January 29, 2008 Author Share Posted January 29, 2008 i echo the surname like this to display the result that i want. <?php echo $row['surname'];?> But it still doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452944 Share on other sites More sharing options...
themistral Posted January 29, 2008 Share Posted January 29, 2008 Try <?php echo $rows['surname'];?> instead of <?php echo $row['surname'];?> You have set the variable as $rows in this line while($rows = mysql_fetch_assoc($res)){ Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452968 Share on other sites More sharing options...
taz321 Posted January 30, 2008 Author Share Posted January 30, 2008 This is my code now <?php $query1 = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'"; $result1 = mysql_query($query1) or die(mysql_error()); while($rows = mysql_fetch_array($result1)){ echo $rows['surname']; } ?> and i display it by doing this (but still doesnt work) <?php echo $rows['surname'];?> But nothing displays Is there anything im doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-452980 Share on other sites More sharing options...
taz321 Posted January 30, 2008 Author Share Posted January 30, 2008 Can anyone help me with this query Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453336 Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 You need to check it actually finds a result. <?php $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { echo $row['surname']; } } else { echo "No results found"; } } else { die("Query failed<br />$sql<br />" . mysql_error()); } ?> What do you get? Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453339 Share on other sites More sharing options...
taz321 Posted January 30, 2008 Author Share Posted January 30, 2008 That still doesnt display the employee surname. :-( This is the code you gave me <?php $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'"; if ($result1 = mysql_query($sql)) { if (mysql_num_rows($result1)) { while ($row = mysql_fetch_array($result1)) { echo $row['surname']; } } else { echo "No results found"; } } else { die("Query failed<br />$sql<br />" . mysql_error()); } ?> And i echo it using this <?php echo $rows['surname'];?> Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453356 Share on other sites More sharing options...
taz321 Posted January 30, 2008 Author Share Posted January 30, 2008 If it helps, this is my whole code for displaying this data. i have indicated below by ''''''' ''''''''''' where i echoed the code where i want the surname to display. <?php session_start(); if (isset($_SESSION['username']) == false){ header("Location:ClientLogin.php"); exit(); } ?> <?php require "connect.php"; $query = "select * from form"; $result = mysql_query($query, $connection) or die ("MySQL Error: ".mysql_error()); ?> <?php $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'"; if ($result1 = mysql_query($sql)) { if (mysql_num_rows($result1)) { while ($row = mysql_fetch_array($result1)) { echo $row['surname']; } } else { echo "No results found"; } } else { die("Query failed<br />$sql<br />" . mysql_error()); } ?> <head> <title>Jupiter Development Support</title> <link href="Images/mystyle.css" rel="stylesheet" type="text/css"> <style type="text/css"> <!-- #Layer2 {position:absolute; width:200px; height:52px; z-index:2; left: 16px; top: 143px; } #Layer1 {position:absolute; width:200px; height:80px; z-index:1; left: 254px; top: 84px; } .style3 {font-family: "Copperplate Gothic Bold"; font-size: x-large; } #Layer5 { position:absolute; width:441px; height:36px; z-index:3; left: 390px; top: 207px; } .style4 {font-size: x-small} .style6 {font-family: "Copperplate Gothic Bold"} #Layer3 { position:absolute; width:156px; height:43px; z-index:4; left: 24px; top: 470px; } #Layer6 { position:absolute; width:126px; height:118px; z-index:4; left: 1079px; top: 186px; } .style7 {font-size: xx-small; } .style8 {font-size: x-small; font-weight: bold; } --> </style> </head> <body> <div id="header"> <h1 align = "center" class="style1"> </h1> <p align = "center" class="style1"> </p> <div id="Layer2"><img src="Images/logo.jpg" width="173" height="43"></div> <p align = "center" class="style1"> </p> <p align = "center" class="style1"> </p> <div id="Layer5"><span class="style3">Analyst Enquiry Screen </span></div> <div id="Layer6"><img src="Images/Symbol.jpg" width="118" height="111"></div> <p> </p> <table width="393" border="0"> <tr> <td width="17"> </td> <td width="495"><span class="style4"><?php echo $_SESSION['firstname']?> <?php echo $_SESSION['surname']?> <span class="style6"> LOGGED IN </span></span></td> </tr> </table> <p> </p> <table width="1582" border="0"> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td width="128"> </td> <td width="125"> </td> <td width="136"> </td> <td width="118"> </td> <td width="131"> </td> <td width="49"> </td> <td width="89" class="style8"> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td class="style8"> </td> <td> </td> <td> </td> </tr> <tr> <td width="64"> </td> <td width="64"> </td> <td width="20"> </td> <td width="68"> </td> <td><div align="center" class="style8">Enquiry Number </div></td> <td><div align="center" class="style8">Issue Title </div></td> <td><div align="center" class="style8">Date Submitted </div></td> <td><div align="center" class="style8">System Affected </div></td> <td><div align="center" class="style8">Priority Level </div></td> <td><div align="center" class="style8"> <div align="center">Team </div> </div></td> <td class="style8"><div align="center">Employee</div></td> <td width="16"> </td> <td width="520"><span class="style4"></span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td width="16"> </td> <td> </td> </tr> <tr> <td width="64"> </td> <td width="64"> </td> <td width="20"> </td> <td width="68"><?php while($row= mysql_fetch_array($result)){?></td> <td height="35"><div align="center" class="style7"><?php echo $row['formID'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['issuetitle'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['datesubmitted'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['systemaffected'];?></div></td> <td height="35"><div align="center"><span class="style7"><?php echo $row['prioritylevel'];?></span></div></td> <td height="35"><div align="center"><span class="style7"><?php echo $row['teamname'];?></span></div></td> '''''''''''''<td height="35"><div align="center" class="style7"><?php echo $row['surname'];?></div></td>''''''''''''''''''''''' <td width="16"> </td> <td><a href="analystformedit.php?formID=<?php echo $row['formID'];?>"> <div align="left"><img src="Images/smallopenbutton.jpg" width="66" height="22"></div></td> <tr> <td width="64"> </td> <td width="64"> </td> <td width="20"> </td> <td width="68"><?php } ?></td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td width="16"> </td> <td width="520"> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td><div align="center"></div></td> <td> </td> <td width="16"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td><div id="Layer3"><a href="Adminlogout.php"><img src="Images/logOffButton.jpg" width="156" height="44" border="0"></a></div></td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td width="16"> </td> <td> </td> </tr> </table> <p> </p> <div id="Layer2"><img src="Images/logo.jpg" width="173" height="43"></div> <div id="Layer1"><img src="Images/JDS.jpg" width="704" height="79"></div> </body> Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453365 Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 Please use tags when posting code. Your code is all over the place. Your second while loop tries to loop through a result called $result, this is never defined anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453380 Share on other sites More sharing options...
taz321 Posted January 30, 2008 Author Share Posted January 30, 2008 <?php while($row= mysql_fetch_array($result)){?></td> <td height="35"><div align="center" class="style7"><?php echo $row['formID'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['issuetitle'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['datesubmitted'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['systemaffected'];?></div></td> <td height="35"><div align="center"><span class="style7"><?php echo $row['prioritylevel'];?></span></div></td> <td height="35"><div align="center"><span class="style7"><?php echo $row['teamname'];?></span></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['surname'];?></div></td> Above is the main code which displays everything i want. And iv realised now wot u mean. Your second while loop tries to loop through a result called $result, this is never defined anywhere But if i change it to $result1 then even though it wouldnt display for all the other fields, should it not still display surname ? Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453390 Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 no, becasue youve already ran that $result1 through a while loop at the top of your script. The would be at the end of its internal pointer. You basically want to replace the last piece of code you posted with... <?php $sql = "SELECT * FROM form JOIN employee ON form.empname = employee.employeeID WHERE empName = 'surname'"; if ($result1 = mysql_query($sql)) { if (mysql_num_rows($result1)) { while ($row = mysql_fetch_array($result1)) {?> <td height="35"><div align="center" class="style7"><?php echo $row['formID'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['issuetitle'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['datesubmitted'];?></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['systemaffected'];?></div></td> <td height="35"><div align="center"><span class="style7"><?php echo $row['prioritylevel'];?></span></div></td> <td height="35"><div align="center"><span class="style7"><?php echo $row['teamname'];?></span></div></td> <td height="35"><div align="center" class="style7"><?php echo $row['surname'];?></div></td> <?php} } else { echo "No results found"; } } else { die("Query failed $sql " . mysql_error()); } ?> And remove the other part from the top. Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453394 Share on other sites More sharing options...
taz321 Posted January 30, 2008 Author Share Posted January 30, 2008 Thanks, il have a little play around with it and let you know. Much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/88396-sql-query/#findComment-453417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.