Joining two tables and displaying results - php/mysql
The following script successfully allows me to search a date and display
all information in the 'JOBS' table relating to the time is was put on the
system which is stored in the 'orderno' table.
// PUT your connection data HERE !
$DB_SERVER = 'localhost';
$DB_NAME = 'database';
$DB_USER = 'username';
$DB_PASS = '*********';
// opening a connection to the database
try
{
$db = new
PDO("mysql:host=".$DB_SERVER.";dbname=".$DB_NAME.";charset=utf8",
$DB_USER, $DB_PASS, array(PDO::ERRMODE_EXCEPTION, PDO::FETCH_ASSOC)
);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$sql =
"SELECT o.JobNumber, o.date_col, Job_Title, Handler
FROM orderno o
INNER JOIN JOBS j on (o.JobNumber = j.JobNo)
WHERE date_col = ?";
// preparing the query
$stmt = $db->prepare($sql);
// assembling the requested date
$date = $_GET['year'] . '-' . $_GET['month'] . '-' . $_GET['day'];
// setting the parameter value
$stmt->bindParam(1, $date, PDO::PARAM_STR);
// executing the query
$stmt->execute();
// returning the resultset
$resultSet = $stmt->fetchAll();
if ( count($resultSet) == 0 ) {
echo "No records found for date ".$date;
}
else {
echo '<table border=1>';
// writing the table header
echo '<tr>';
foreach($resultSet[0] as $key => $value) {
if (!is_numeric($key)) {
echo '<TH>'.$key.'</TH>';
}
}
echo '</tr>';
// writing the rows...
foreach($resultSet as $row) {
echo '<tr>';
// each field...
for($i = 0; $i < $stmt->columnCount(); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}
echo '</table>';
}
?></center>
However, in the 'JOBS table, the 'Handler' is stored as a number. In the
'handler' table it shows the relating number but matches it with the
FullName.
How do I link this script to the 'handler table' and display the FullName
in this column rather than the number which is stored in the JOBS table?
Thank you!
No comments:
Post a Comment