Hello again
Thanks for your quick response.
I have tried to use this code but the page comes back with a ...Invalid argument supplied for foreach()...
I think i have had this error when nothing is retrieved from the database?
The definitions I have plugged into your code are exactly the ones I used (and got some kind of response) with my previous code.
just fyi my Database is called machines as is the Table I am trying to query (sorry!)
is it something I have not copied correctly in the $pdo statement? although I have gone thru it with a fine toothpick.
To keep it simple I have put your connection code and the table code into one simple file.:
do you have a snippet of error checking code I could plug in to check the connection etc.
Thanks for your help - appreciated.
I set myself the task of learning php/mysql and it looks like I now need to learn pdo.
<?php
//definitions for the connection:
define('DSN','mysql:host=localhost;dbname=machines'); //pdo dsn
define('USER','root'); //pdo user
define('PW','xxx'); //pdo password
//connect to the database:
$pdo = new PDO(DSN,USER,PW,array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC)); //setting the default fetch mode to associative array.
$table = '<table cellspacing="3" cellpadding="3">'; //start our table, I usually hold the building in a variable, for output later.
$query = "SELECT MachineName ,BedSizeX, FROM machines"; //This is the query we send to mysql.
$i = 0; //This is a control variable.
foreach($pdo->query($query) as $row)
{ //since we use a standard query, you can call it in a foreach to get each row.
if($i == 0)
{
$keys = array_keys($row); //we get the column names.
$table .= '<tr><th>' . implode('</th><th>',$keys) . '</th></tr>'; //and add them as table headers.
++$i; //then increment our control so that this block will not run again.
}
$table .= '<tr><td>' . implode('</td><td>',$row) . '</td></tr>'; //then we populate the fields of the table with our data.
}
$table .= '</table>'; //Then we end the table.
echo $table;
?>