-
-
Save lucasdamianjohnson/1a1aaa98d1fcdb8a43a95c4218f3512a to your computer and use it in GitHub Desktop.
PHP PDO Example
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 | |
/** PHP PDO Example | |
* Used for quick copy past access to a database with PDO. | |
*/ | |
$charset = "utf8"; | |
$database = "db1"; | |
$dsn = "mysql:host=$mysql_server;charset=$charset"; | |
$options = [ | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
]; | |
try { | |
$pdo = new PDO($dsn, $mysql_user, $mysql_password, $options); | |
} catch (\PDOException $e) { | |
echo $e->getMessage(); | |
die(); | |
} | |
try { | |
$stmt = $pdo->query("SELECT * FROM $database.sometable"); | |
} catch (\PDOException $e) { | |
echo $e->getMessage(); | |
} | |
foreach ($stmt as $row) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment