BarclayVision BarclayVision - 4 months ago 10
MySQL Question

mysql two different queries into two different html tables

I want to run two different mysql queries and output results into two different html tables. I'm opening one DB connection and fetching two entirely different result sets.

I have one page that I want to show two different 's in the page, each table is the results from different query.

echo "<table class='table table-striped table-bordered table-hover table-condensed'>";
echo "<thead><tr>";
echo "<th>Last</th><th>First</th><th>MDC</th><th>RADIO</th><th>EPCR</th><th>FH</th>";
echo "</tr></thead></table>";

while ($rowA = mysql_fetch_array($result)) {
echo "<tbody><tr>";
echo "<td>".$rowA['LAST']."</td>";
echo "<td>".$rowA['FIRST']."</td>";
echo "<td>".$rowA['MDC']."</td>";
echo "<td>".$rowA['RADIO']."</td>";
echo "<td>".$rowA['ePCR']."</td>";
echo "<td>".$rowA['Firehouse']."</td>";
}
echo "</tr></tbody></table>";

echo "<table class='table table-striped table-bordered table-condensed'>";
echo "<thead><tr>";
echo "<th>USERNAME</th><th>CLASSNAME</th><th>DATE COMPLETED</th>";
echo "</tr></thead></table>";


while ($rowB = mysql_fetch_array($sql)) {
echo "<tbody><tr>";
echo "<td>".$rowB['UserName']."</td>";
echo "<td>".$rowB['ClassName']."</td>";
echo "<td>".$rowB['DateCompleted']."</td>";
}
echo "</tr></tbody></table>";

mysql_close($dbhandle);


here is my query:

$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");



$selected = mysql_select_db("tech_training",$dbhandle)
or die("Could not select examples");


$result = mysql_query("SELECT LastName AS LAST, FirstName AS FIRST,
MAX(IF(`ClassName`='MDC (Intro)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'MDC',
MAX(IF(`ClassName`='800 MHz Radio (Intro)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'RADIO',
MAX(IF(`ClassName`='ePCR (Intro)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'ePCR',
MAX(IF(`ClassName`='Firehouse (Incident)', DATE_FORMAT(`DateCompleted`, '%m/%d/%Y'), NULL)) AS 'Firehouse'
FROM EnrollmentsTbl INNER JOIN UsersDataTbl ON EnrollmentsTbl.UserName = UsersDataTbl.UserName
GROUP BY EnrollmentsTbl.UserName
ORDER BY LastName
LIMIT 20;");


//execute the second SQL query and return records
$sql = mysql_query("SELECT UserName, ClassName, DateCompleted FROM EnrollmentsTbl LIMIT 10;");

Answer

Somewhere at the top of the page, or preferably use an include statement since according to PSR 1 you should not have output mixed with functions/classes.

Use PDO and here is an example.

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

$stmt = $db->query('SELECT * FROM table');
$rowA = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $db->query('SELECT * FROM table2');
$rowB = $stmt->fetchAll(PDO::FETCH_ASSOC);

ANSWER

echo "<tbody>"; //this is outside
while ($rowB = mysql_fetch_array($sql)) {
    echo "<tr>";
    echo "<td>".$rowB['UserName']."</td>";
    echo "<td>".$rowB['ClassName']."</td>";
    echo "<td>".$rowB['DateCompleted']."</td>";
    echo "</tr>";// MOVE THIS INSIDE THE LOOP!!!

}
echo "</tbody></table>";

other loop:

echo "<tbody>"; //outide
while ($rowA = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>".$rowA['LAST']."</td>";
    echo "<td>".$rowA['FIRST']."</td>";
    echo "<td>".$rowA['MDC']."</td>";
    echo "<td>".$rowA['RADIO']."</td>";
    echo "<td>".$rowA['ePCR']."</td>";
    echo "<td>".$rowA['Firehouse']."</td>";
    echo "</tr>"; //move end row here
}

echo "</tbody></table>";     

EXPLANATION

//you have some array
$result = [];

echo "<table>" // you only want one of these
foreach($results as $rows){
    echo "<tr>";
    echo "<td>data</td><td> mode data </td>";
    echo "</tr>"; close row each time
}
echo "</table>" //close it ONLY ONCE!