I want to get details from two table in my database.I use following code
$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>";
}
after going to get data then an error occurred
Notice: Undefined index: student_detail.reg_no in C:\\xampp\\htdocs\\HMS\\HMS\\lib\\HOS_current.php on line 47
How can I fix it?
I would suggest that you use sql joins, SQL joins are used to combine rows from two or more tables.
This is how to achieve what you want with joins
$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>";
}
?>
In the above I have used inner join
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
The basic syntax for this join is:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
NB: INNER JOIN is the same as JOIN.
NB: When you printout the results, don't echo $row['tableName.ColumnName'];
just echo out $row['ColumnName'];
You have a wrong single quote at the beginning for student_detail.reg_no change it with a backtics
You have a wrong single quote at the beginning for student_detail.reg_no change it with a backtics
ad the bakctics should wrap table and column distincly and you have backtics arount the tow tables too
$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`; ";
or, due the fact that you have not reserved word or space, don't use backics in this case
$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; ";
for the second error do the fatc you are in while loop mean the query should return rows .. so the error could be related to the column name ... try (for test) using an alias eg:
SELECT
student_detail.reg_no as no
.....
and refer to you column in while loop this way
echo"<tr id=\"".$row['no']."\" onclick=\"Redirect(this.id)\"><td>".$row['no'].
"</td><td></td><td></td></tr>";
Try:
$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;";
See the ANSI JOIN syntax? Also, bad backticks and apostophes in orginal code
If you really have to use backticks, use them around each entity:
`student_detail`.`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; ";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.