【发布时间】:2015-04-21 16:23:27
【问题描述】:
朋友我有3张桌子如下
create table departments
(
department_id int(20) AUTO_INCREMENT primary key,
department_name varchar(30) not null
);
create table designations
(
designation_id int(20) AUTO_INCREMENT primary key,
designation_name varchar(30) not null
);
create table employees
(
employee_id int(20) AUTO_INCREMENT primary key,
employee_name varchar(30) not null,
department int,
designation int,
salary int(20) not null,
FOREIGN KEY (department) REFERENCES departments(department_id),
FOREIGN KEY (designation) REFERENCES designations(designation_id),
);
这是我的 3 张桌子。现在我需要查看下面的员工表
员工ID |员工姓名 |指定名称 |部门名称 |员工工资
我如何得到它?以下是我的错误程序
<?php
$database_connection = new mysqli("localhost","root","","employee_application");
if($database_connection->connect_error)
die ("connection failed".$database_connection->connect_error);
$sql_query = "select employees.employee_name,employees.designation,employees.department,employees.employee_salary,departments.department_id,designations.designation_id
from employees,departments,designations
where employees.department = departments.department_id and employees.designation = designations.designation_id";
$result=$database_connection->query($sql_query);
if($result->num_rows > 0){
echo "<table>";
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<td>".$rows["department"]."<td>";
echo "<td>".$rows["designation"]."<td>";
echo "<td>".$rows["salary"]."<td>";
echo "<tr>";
}
echo "</table>";
}
else{
echo "Table is empty";
}
?>
【问题讨论】:
-
你遇到了什么错误?
-
限制超出,我猜 while 循环语句是错误的
标签: php mysql select foreign-key-relationship