note 130053 added to ref.pdo-mysql.connection
| From: | dashsupercraft2@gmail.com | Date: | Sat, 22 Feb 2025 21:08:12 +0000 |
| Subject: | note 130053 added to ref.pdo-mysql.connection | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221877@lists.php.net to get a copy of this message | ||
This is a good way to initiate a MySQL connection
<?php
$host = "db";
$db = "mydatabase";
$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$user = "user";
$passwd = "password";
try {
$pdo = new PDO($dsn, $user, $passwd, $options);
error_log("Successful");
} catch (PDOException $e) {
error_log("Error: " . $e->getMessage());
die(json_encode(["error" => "Connection error " . $e->getMessage()]));
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 142.137.165.170)
----
Manual Page -- https://php.net/manual/en/ref.pdo-mysql.connection.php
Edit -- https://main.php.net/note/edit/130053
Del: integrated -- https://main.php.net/note/delete/130053/integrated
Del: useless -- https://main.php.net/note/delete/130053/useless
Del: bad code -- https://main.php.net/note/delete/130053/bad+code
Del: spam -- https://main.php.net/note/delete/130053/spam
Del: non-english -- https://main.php.net/note/delete/130053/non-english
Del: in docs -- https://main.php.net/note/delete/130053/in+docs
Del: other reasons-- https://main.php.net/note/delete/130053
Reject -- https://main.php.net/note/reject/130053
Search -- https://main.php.net/manage/user-notes.php