Skip to content

Instantly share code, notes, and snippets.

@dcblogdev
Created July 23, 2013 13:00
Show Gist options
  • Save dcblogdev/6062192 to your computer and use it in GitHub Desktop.
Save dcblogdev/6062192 to your computer and use it in GitHub Desktop.
display demo with PDO
<?php
//connection details
$host = 'localhost';
$dbname = 'database name';
$dbusername = 'database username';
$password = 'db password';
//make a connection
$db = new PDO("mysql:$host=;port=8889;$dbname=", $dbusername, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//get records from database if there's an error catch it.
try {
//select columns to select from the database table
$stmt = $db->query('SELECT id, name, address FROM table');
//loop through the rows
while($row = $stmt->fetch()){
//print the output
echo '<p>';
echo 'id: '.$row['id'].'<br />';
echo 'name: '.$row['name'].'<br />';
echo 'addres: '.$row['address'];
echo '</p>';
}// end the while
} catch(PDOException $e) {
//display any error messages
echo $e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment