Php 使用两个表获取一个查询
我想从数据库中的两个表中获取详细信息Php 使用两个表获取一个查询,php,sql,Php,Sql,我想从数据库中的两个表中获取详细信息 $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.roo
$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>";
}
$sqlall=“选择
学生详细信息。注册号
,学生详情。全名
,学生详情。学习年份
,学生/教师
,学生,课程详情
,学生宿舍。注册号
,学生宿舍,房间号
,学生宿舍
来自学生宿舍详细信息,学生宿舍
其中student_detail.reg_no=student_hotel.reg_no;”;
//这是打印部分
$result=$conn->query($sqlall);
如果($result->num_rows>0){
//每行的输出数据
//创建表
回声“
登记号:旅馆idRoom No”;
而($row=$result->fetch_assoc()){
回音“$row[“学生宿舍详细信息.登记号]”。$row[“学生宿舍.房间号”]。$row[“学生宿舍.房间号”]。”;
}
回声“;
}
其他的
{
回声“
登记号旅舍idRoom号;
";
}
获取数据后发生错误
注意:第47行C:\xampp\htdocs\HMS\HMS\lib\HOS\u current.php中的未定义索引:student\u detail.reg\u 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 INNER JOIN student_hostel ON 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 INNER JOIN student_hostel ON student_detail.reg_no=student_hostel.reg_no; ";
您在student\u detail.reg\u的开头有一个错误的单引号,请不要用backtics更改它
您在student\u detail.reg\u的开头有一个错误的单引号,请不要用backtics更改它
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`; ";
或者,由于您没有保留字或空格,在这种情况下不要使用backics
$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; ";
对于第二个错误,while循环中的fatc是否意味着查询应该返回行。。因此错误可能与列名有关。。。尝试使用别名(用于测试),例如:
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>";
echo'.$row['no']。
"";
您在student\u detail.reg\u的开头有一个错误的单引号,请不要使用backtics更改它
您在student\u detail.reg\u的开头有一个错误的单引号,请不要用backtics更改它
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`; ";
或者,由于您没有保留字或空格,在这种情况下不要使用backics
$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; ";
对于第二个错误,while循环中的fatc是否意味着查询应该返回行。。因此错误可能与列名有关。。。尝试使用别名(用于测试),例如:
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>";
echo'.$row['no']。
"";
试试:
查看ANSI连接语法?此外,原始代码中的反勾号和撇号也很糟糕
如果确实必须使用反勾号,请在每个实体周围使用它们:
`student_detail`.`reg_no`
尝试:
查看ANSI连接语法?此外,原始代码中的反勾号和撇号也很糟糕
如果确实必须使用反勾号,请在每个实体周围使用它们:
`student_detail`.`reg_no`
我建议您使用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>";
}
?>
或
NB:内部联接与联接相同。
注意:打印结果时,不要
echo$row['tableName.ColumnName']代码>只需回显$row['ColumnName']代码>我建议您使用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>";
}
?>
或
NB:内部联接与联接相同。
注意:打印结果时,不要echo$row['tableName.ColumnName']代码>只需回显$row['ColumnName']代码>尝试这并不能真正帮助OP了解他做错了什么尝试这并不能真正帮助OP了解他做了什么wrong@JohnHC . 我已经明白了。。我已经更新了答案。。无论如何,非常感谢您的建议echo.“$row[“student_detail.reg_no”]。$row[“student_hostel.room_no”]。”.$row[“student_hostel.room_no”]。”;}
注意:未定义索引:C:\xampp\htdocs\HMS\HMS\lib\HOS\u current.php第47行中的student\u detail.reg\n您提供的代码仅用于sql.query。如果要查看结果,请输入注释。。因此,您应该更新您的问题,并添加用于呈现结果的代码。@RUC。。。我已经用一些建议更新了答案,希望是useful@JohnHC . 我已经明白了。。我已经更新了答案。。无论如何,非常感谢您的建议echo.“$row[“student_detail.reg_no”]。$row[“student_hostel.room_no”]。”.$row[“student_hostel.room_no”]。”;}
注意:未定义索引:C:\xampp\htdocs\HMS\HMS\lib\HOS\u current.php第47行中的student\u detail.reg\n您提供的代码仅用于sql.query。如果要查看结果,请输入注释。。因此,您应该更新您的问题,并添加用于呈现结果的代码。@RUC。。。我用一些建议更新了答案,希望有用。您将不得不发布更多内容,然后请只回显不带表名的行echo$row['reg_no']代码>运行查询时用代码编辑问题以及如何显示更新的我的答案请检查。您必须发布更多内容,然后请只回显不带表名的行echo$row['reg\u no']代码>运行查询时用代码编辑问题以及如何显示更新的我的答案请检查。