I there
Im using PDO and php to select table data from a MySql table on my web site
this is my php script and it does work as i want.... But the bit i don't like is hard coding the titles is there no way that i can get that data from the results??
Ian
Im using PDO and php to select table data from a MySql table on my web site
Code:
<?php
if (isset($_GET['a']) != '')
{
$vehicleid = $_GET['a'];
$select = "SELECT InvoiceNumber,InvoiceDate,Amount FROM InvoicesView WHERE VehicleId = ".$vehicleid ;
try
{
$con = new PDO('mysql:host=mjt.mooo.com;dbname=MJTGarage;charset=utf8','ian','urksworks');
}
catch (PDOException $ex)
{
$error = true;
echo $ex->getMessage();
}
$st = $con->prepare($select);
$response = $st->execute();
try
{
if ($response === true)
{
$titles = array('InvoiceNumber', 'InvoiceDate', 'Cost'); //<---------------- hard coded here
$html_table = '<table border="1" cellspacing="0" cellpadding="2"><tr>';
foreach($titles as $title)
{
$html_table .= "<th> $title </th>";
}
$html_table .='</tr>';
$rows = $st->fetchALL(PDO::FETCH_ASSOC);
foreach( $rows as $row )
{
$html_table .= '<tr>' . "\n";
foreach( $row as $col )
{
$html_table .= '<td>' .$col. '</td>';
}
$html_table .= '</tr>' . "\n";
}
echo $html_table;
}
else
{
$response["success"] = 0;
$response["message"] = $ex->getMessage();
echo json_encode($response);
}
}
catch (Exception $oex)
{
echo $oex->getMessage();
}
}
?>
Ian