Lets say I have two tables contacts
and departments
.
The contents of the table are as follows:
contacts:
id | first_name | last_name | email | department_id
10 | Mani | Raj | raj@mail.com | 2
11 | Santhos | Sam | santhos@mail.com | 3
departments:
id | department_name
1 | HR
2 | Sales
3 | Finance
I'm trying to print the details of the tables using INNER JOIN
My query is:
$sql_query = "SELECT * FROM contacts INNER JOIN departments, departments.id as departments.department_id ON contacts.department_id=departments.department_id ORDE`R BY contacts.id";
I've referred the question below: SQL exclude a column using SELECT * [except columnA] FROM tableA?
but I don't want to create a temporary table.
I was thinking may be there was a way for alaising the id column from the department table.
Add
I could print the elements from the tables using the query:
$sql_query = "SELECT * FROM $table_name INNER JOIN departments ON $table_name.department_id=departments.department_id ORDER BY $table_name.id";
It works great but the problem is when I try to print the details on my web page, somehow the id
from the contacts
table is overwitten by the id
from the departments
table.
code that I use to print the table is:
if ($query_result->num_rows > 0) {
?>
<table>
<tr>
<th>ID</th>
<th> First Name </th>
<th> Last Name </th>
<th>E-mail</th>
<th>Department</th>
</tr>
<?php
while($row = $query_result->fetch_assoc()) {
?>
<tr>
<td> <?php echo $row["id"]; ?> </td>
<td> <?php echo $row["first_name"]; ?> </td>
<td> <?php echo $row["last_name"]; ?> </td>
<td> <?php echo $row["email"]; ?> </td>
<td> <?php echo $row["department_name"]; ?> </td>
</tr>
<?php
}
?>
</table>
<?php
}
The output of this code is :
ID | First Name | Last Name | E-mail | Department
2 | Mani | Raj | mani@mail.com | Sales
8 | Santhos | Sam | santhos@mail.com | Finance
whereas my output should be:
ID | First Name | Last Name | E-mail | Department
10 | Mani | Raj | mani@mail.com | Sales
11 | Santhos | Sam | santhos@mail.com | Finance
Now how would I go about printing the values without being overwritten?
I'm a newbie. Please help me...
Edit
OK,cool this works
$sql_query = "SELECT *, contacts.id as id FROM contacts INNER JOIN departments
ON contacts.department_id=departments.department_id ORDER BY contacts.id";
But I'm also trying to export the details to a .xls file. I'm getting an error there!!
This is my error
You have an error in your SQL syntax check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as id FROM INNER JOIN departments ON .department_id=departments.id ORDER BY .id' at line 1<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/opt/lampp/htdocs/contacts/export.php</b> on line <b>23</b><br />
and my code is:
$flag = false;
foreach($query_result as $row) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, __NAMESPACE__ . '\cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
function cleanData(&$str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}