I had been separated two table from the data which is students room's booking record. There are two tables, "Applicant" is stored students' ID and nickname, and "rmBooking" is stored the records of booking(time,date etc).
-Table Applicant
user_id | name
---------------------
1 | Benny
2 | Chars
3 | Harry
...... | ......
-Table rmBooking
room | booking_date | from_time | to_time
----------------------------------------------------
101 | 22-09-2014 | 1 | 3
101 | 28-09-2014 | 2 | 4
101 | 02-10-2014 | 1 | 4
101 | 04-10-2014 | 3 | 6
...... | ...... | ...... | ......
I would like to show the results as one table in browser, just like:
user_id | name | room | booking_date | from_time | to_time
-----------------------------------------------------------------------------
1 | Benny | 101 | 22-09-2014 | 1 | 3
2 | Chars | 101 | 28-09-2014 | 2 | 4
3 | Harry | 101 | 02-10-2014 | 1 | 4
...... | ...... | ...... | ...... | ...... | ......
When I tried to do this work and test int he browser, the table showed:
user_id | name | room | booking_date | from_time | to_time
-----------------------------------------------------------------------------
1 | Benny | 101 | 22-09-2014 | 1 | 3
1 | Benny | 101 | 22-09-2014 | 1 | 3
1 | Benny | 101 | 22-09-2014 | 1 | 3
...... | ...... | ...... | ...... | ...... | ......
2 | Chars | 101 | 22-09-2014 | 1 | 3
2 | Chars | 101 | 22-09-2014 | 1 | 3
...... | ...... | ...... | ...... | ...... | ......
3 | Harry | 101 | 22-09-2014 | 1 | 3
3 | Harry | 101 | 22-09-2014 | 1 | 3
...... | ...... | ...... | ...... | ...... | ......
1 | Benny | 101 | 28-09-2014 | 2 | 4
1 | Benny | 101 | 28-09-2014 | 2 | 4
...... | ...... | ...... | ...... | ...... | ......
2 | Chars | 101 | 28-09-2014 | 2 | 4
2 | Chars | 101 | 28-09-2014 | 2 | 4
...... | ...... | ...... | ...... | ...... | ......
..............................................................
1 | Benny | 101 | 02-10-2014 | 1 | 4
...... | ...... | ...... | ...... | ...... | ......
And my PHP code:
<?php
$dbConnection = mysql_connect("localhost", "aaaaa", "bbbbb");
if (!$dbConnection) {
die("Could not connect database: " . mysql_error());
}
mysql_select_db("aaaaa");
$show_rmBooking = mysql_query("SELECT DISTINCT user_id, name, booking_date, from_time, to_time, room FROM rmBooking, Applicant
GROUP BY user_id
ORDER BY user_id, booking_date;
");
if(!$show_rmBooking){
die("Cannot select Database: rmBooking or Applicant, Error:" . mysql_error());
}
$rmTable = "<table border=1>";
$rmTable .= "<tr>";
$rmTable .= "<th colspan=6 align=center>Room's Enrollment report</th>";
$rmTable .= "</tr>";
$rmTable .= "<tr>";
$rmTable .= "<td style='text-align:center'>ID</td>";
$rmTable .= "<td style='text-align:center'>name</td>";
$rmTable .= "<td style='text-align:center'>Room Number</td>";
$rmTable .= "<td style='text-align:center'>Booking date</td>";
$rmTable .= "<td style='text-align:center'>From</td>";
$rmTable .= "<td style='text-align:center'>To</td>";
$rmTable .= "</tr>";
$rmTable .= "<tr>";
if(mysql_num_rows($show_rmBooking) > 0){
while ($rows_Applicant = mysql_fetch_array($show_rmBooking)){
foreach($rows_Applicant as $key => $value){
$rmTable .= "<tr>";
$rmTable .= "<td>" . $rows_Applicant['user_id'] . "</td>";
$rmTable .= "<td>" . $rows_Applicant['name'] . "</td>";
$rmTable .= "<td>" . $rows_Applicant['room'] . "</td>";
$rmTable .= "<td>" . $rows_Applicant['booking_date'] . "</td>";
$rmTable .= "<td>" . $rows_Applicant['from_time'] . "</td>";
$rmTable .= "<td>" . $rows_Applicant['to_time'] . "</td>";
$rmTable .= "</tr>";
}
}
}
$rmTable .= "</tr>";
$rmTable .= "</table>";
echo $rmTable;
mysql_free_result($show_rmBooking);
mysql_close($dbConnection);
?> It shows the wrong results and the booking records are duplicating with the students. Shall I need to join the tables first in mysql server or coding incorrectly? Can anyone teach me how to join the tables in mysql, thanks a lot.
mysql_*functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.