I'm a beginner in PHP and MySQL and I'm still in learning process. Is it possible to combine many MySQL statement into one query? This is because I want to display every new output into new row in a table. The coding below is not the right way to display the output like what I want.
<table>
<tr>
<th>Staff Name</th>
<th>Staff No.</th>
<th>Grade</th>
<th>Position</th>
<th>Department</th>
</tr>
<tr>
<?php
$query="select staffName, staffNo from tblstaff";
$result=mysql_query($query) or die (mysql_error());
while($row= mysql_fetch_array($result))
{
?>
<td><?php echo $row['staffName']; ?></td>
<td><?php echo $row['staffNo']; ?></td>
<?php
}
?>
<?php
$query="select grade, gradePosition, gradeDepartment from tblgrade";
$result=mysql_query($query) or die (mysql_error());
while($row= mysql_fetch_array($result))
{
?>
<td><?php echo $row['grade']; ?></td>
<td><?php echo $row['gradePosition']; ?></td>
<td><?php echo $row['gradeDepartment']; ?></td>
<?php
}
?>
</tr>
</table>
The result for the above code is all the staff name and staff no in database table out first and then follow by all grade, position and department in a database table displayed in a row of the table. The output is not sequence accordingly like I want. Can anyone help me to solve my problem?
Thank you in advance.
</div>
Yes it is possible. Use mysqli_multi_query() function.
Two options, INNER and LEFT joins can join two tables togeather.
INNER JOIN will only display results if data for the records exists in both table, IE, only staff with position and department.
LEFT JOIN will get all staff, and fill in the other table information if it is available.
SELECT staffName, s.staffNumber, grade, gradePosition, gradeDepartment
FROM tblstaff s
(INNER/OR/LEFT) JOIN tblgrade g on s.staffNumber = g.staffNumber
This solution requires a relation between the two tables, which they should have if they are related.