Results 1 to 2 of 2

Thread: col names using pdo

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Reading, UK
    Posts
    192

    col names using pdo

    I there

    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();
    		}
    		
    	}
    ?>
    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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width