Reputation: 825
As a beginner I'm not fan of concatenating but I think this is something that would be really useful for me to know. So I have a while statement that gets data from a table in a database. I'm using a second query below and in between that while statement to display extra information from a different table.
<?php while($employeeproject=mysqli_fetch_array($resultemp)){ ?>
<td data-th="Employee">
<!-- INNER JOIN EMPLOYEE TO GET NAMES -->
<?php $sqlemps="SELECT first_name, last_name FROM employees INNER JOIN employeeprojects ON employees.employee_id=employeeprojects.employee_id";$resulempls=mysqli_query($db,$sqlemps);$displayempnames = $resulempls->fetch_assoc();?>
<?=$displayempnames['first_name'];?> <?=$displayempnames['last_name'];?>
</td>
<?php };?>
The while statement gets info from employeeprojects
and I display names joining with employees
table. It works but problem is the program displays the same name even if it's a different employee_id
. I'd like to concatenate and add a WHERE clause in the second query but im not sure how.
Something like...
$sqlemps="SELECT first_name, last_name FROM employees INNER JOIN employeeprojects ON employees.employee_id=employeeprojects.employee_id WHERE employee_id=<?=$employeeproject['employee_id'];?>";
Obviously this will not work so how do I concatenate?
Upvotes: 0
Views: 669
Reputation: 23892
You shouldn't join variables to queries, you should parameterize and bind them...but you don't need to do any of that. You can do this all with one query using multi joins.
SELECT first_name, last_name, project_name, client_name
from projects as p
join employeeprojects as ep
on p.id = ep.project_id
joiN employees as e
on ep.employee_id = e.employee_id
Demo: http://sqlfiddle.com/#!9/19630e/2
Upvotes: 2
Reputation: 23
No need of concatenation
$empID = $employeeproject['employee_id'];
$sqlemps="SELECT first_name, last_name FROM employees INNER JOIN employeeprojects ON employees.employee_id=employeeprojects.employee_id WHERE employee_id= $empID";
Upvotes: -1