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;"); 


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);


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>";// MOVE THIS INSIDE THE LOOP!!!


Category: php Time: 2016-07-29 Views: 0
Tags: php mysql

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.206 (s). 12 q(s)