我想从我的数据库中的两个表中获取详细信息。我使用以下代码
$sqlall="SELECT
student_detail.reg_no
, student_detail.full_Name
, student_detail.year_of_study
, student_detail.faculty
, student_detail.course
,student_hostel.reg_no
,student_hostel.room_no
,student_hostel.hostel_id
FROM student_detail,student_hostel
WHERE student_detail.reg_no = student_hostel.reg_no; ";
//This is print part
$result = $conn->query($sqlall);
if ($result->num_rows > 0) {
// output data of each row
//create table
echo "<table class=\"table table-hover\">
<tr>
<th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>";
while($row = $result->fetch_assoc()) {
echo"<tr id=\"".$row["student_detail.reg_no"]."\" onclick=\"Redirect(this.id)\"><td>".$row["student_detail.reg_no"]."</td><td>".$row["student_hostel.room_no"]."</td><td>".$row["student_hostel.room_no"]."</td></tr>";
}
echo "</table>";
}
else
{
echo "<table class=\"table table-hover\">
<tr>
<th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>;
<span id></span>
</table>";
}
获取数据后发生错误
注意:未定义的索引:第47行的C:\ xampp \ htdocs \ HMS \ HMS \ lib \ HOS_current.php中的student_detail.reg_no
我该如何解决?
答案 0 :(得分:2)
我建议您使用sql连接,SQL连接用于组合来自两个或多个表的行。
这是如何用连接实现你想要的东西
$sqlall = "SELECT DISTINCT student_detail.reg_no, student_detail.full_Name, student_detail.year_of_study, student_detail.faculty, student_detail.course,student_hostel.reg_no,student_hostel.room_no,student_hostel.hostel_id FROM student_detail inner join student_hostel on student_hostel.reg_no
= student_detail.reg_no";
$result = $conn->query($sqlall);
if ($result->num_rows > 0) {
// output data of each row
//create table
echo "<table class=\"table table-hover\">
<tr>
<th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>";
while($row = $result->fetch_assoc()) {
echo"<tr id=\"".$row["reg_no"]."\" onclick=\"Redirect(this.id)\"><td>".$row["reg_no"]."</td><td>".$row["room_no"]."</td><td>".$row["room_no"]."</td></tr>";
}
echo "</table>";
}
else
{
echo "<table class=\"table table-hover\">
<tr>
<th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>;
<span id></span>
</table>";
}
?>
在上面我使用了inner join
INNER JOIN关键字选择两个表中的所有行 两个表中的列之间存在匹配。
此联接的基本语法是:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
或
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
注意:INNER JOIN与JOIN相同。
注意:当您打印出结果时,请不要echo $row['tableName.ColumnName'];
回复$row['ColumnName'];
答案 1 :(得分:1)
你在开头有一个错误的单引号student_detail.reg_no用一个背景来改变它
你在student_detail.reg_开头有一个错误的单引号,不能用背叛来改变它 广告bakctics应该包括表和列 并且你也会在两个桌子上引起反击
$sqlall="SELECT
`student_detail`.`reg_no`
, `student_detail`.`full_Name`
, `student_detail`.`year_of_study`
, `student_detail`.`faculty`
, `student_detail`.`course`
,`student_hostel`.`reg_no`
,`student_hostel`.`room_no`
,`student_hostel`.`hostel_id`
FROM student_detail,student_hostel
WHERE `student_detail`.`reg_no` = `student_hostel`.`reg_no`; ";
或者,由于您没有保留字或空格,在这种情况下不要使用背景
$sqlall="SELECT
student_detail.reg_no
, student_detail.full_Name
, student_detail.year_of_study
, student_detail.faculty
, student_detail.course
,student_hostel.reg_no
,student_hostel.room_no
,student_hostel.hostel_id
FROM student_detail,student_hostel
WHERE student_detail.reg_no = student_hostel.reg_no; ";
对于第二个错误执行你所处理的fatc而while循环意味着查询应该返回行..所以错误可能与列名相关...尝试(用于测试)使用别名,例如:
SELECT
student_detail.reg_no as no
.....
以这种方式在while循环中引用你的列
echo"<tr id=\"".$row['no']."\" onclick=\"Redirect(this.id)\"><td>".$row['no'].
"</td><td></td><td></td></tr>";
答案 2 :(得分:1)
尝试:
$sqlall= "
SELECT student_detail.reg_no,
student_detail.full_Name,
student_detail.year_of_study,
student_detail.faculty,
student_detail.course,
student_hostel.reg_no,
student_hostel.room_no,
student_hostel.hostel_id
FROM student_detail
INNER JOIN student_hostel
ON student_detail.reg_no = student_hostel.reg_no;";
请参阅ANSI JOIN语法?此外,原始代码中的坏反引号和撇号
如果确实必须使用反引号,请在每个实体周围使用它们:
`student_detail`.`reg_no`
答案 3 :(得分:0)
尝试:
$sqlall="SELECT student_detail.reg_no, student_detail.full_Name, student_detail.year_of_study, student_detail.faculty, student_detail.course,student_hostel.reg_no,student_hostel.room_no,student_hostel.hostel_id FROM student_detail INNER JOIN student_hostel ON student_detail.reg_no=student_hostel.reg_no; ";