I have a 2 tables:
Category
with Primary KeyID
and columnName
Employee
with Primary KeyID
and columnCategory_id
Note: Category_id
now displays ID
correctly
I want to show Name
instead of ID
for output from Employee
.
Attempt:
$categ = mysql_query("SELECT * FROM employee WHERE id = '" . $_GET['id'] . "'");
$rows = array();
while ($row = mysql_fetch_assoc($categ)) {
$website_cat = $row;
}
Category
Table:
+----+----------------+
| ID | Name |
+----+----------------+
| 23 | Manager |
| 10 | Boss |
| 14 | Worker |
| 41 | Another |
+----+----------------+
Employee
Table:
+----+----------------+
| ID | Category_id |
+----+----------------+
| 1 | Manager |
| 2 | Boss |
| 3 | Worker |
| 4 | Another |
+----+----------------+
Output:
echo $website_cat['category_id'];
My Answer:
To show the name instead of the ID from the Category table in the output, you need to join the Employee table with the Category table based on the Category_id. Here is an example query that you can use:
php
$categ = mysql_query("SELECT e.ID, c.Name as Category_Name FROM employee e
JOIN category c ON e.Category_id = c.ID
WHERE e.ID = '" . $_GET['id'] . "'");
$rows = array();
while ($row = mysql_fetch_assoc($categ)) {
$website_cat = $row;
}
echo $website_cat['Category_Name'];
This query will fetch the Category Name from the Category table based on the Category_id in the Employee table and display it in the output.
Rate this post
4 of 5 based on 5332 votesComments