Created
July 23, 2013 13:00
-
-
Save dcblogdev/6062192 to your computer and use it in GitHub Desktop.
display demo with PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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