note 128169 added to pdo.construct
| From: | theking2 at king dot ma | Date: | Mon, 30 Jan 2023 10:27:02 +0000 |
| Subject: | note 128169 added to pdo.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220125@lists.php.net to get a copy of this message | ||
A generic pattern to connect to a mariadb or mysql database using a settings file
<?php
$_SETTINGS = parse_ini_file('./settings.ini', true);
$db = new \PDO(
"mysql:hostname={$_SETTINGS['db']['host']};dbname={$_SETTINGS['db']['name']}",
$_SETTINGS['db']['user'],
$_SETTINGS['db']['pass'],
[
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'"
]
);
?>
You might not need the options depicted here but I find them convenient.
Works with a settings.ini file containing for instance:
[db]
host = "localhost"
name = "dbname"
user = "dbuser"
pass = "dbpassword"
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 83.97.81.249)
----
Manual Page -- https://php.net/manual/en/pdo.construct.php
Edit -- https://main.php.net/note/edit/128169
Del: integrated -- https://main.php.net/note/delete/128169/integrated
Del: useless -- https://main.php.net/note/delete/128169/useless
Del: bad code -- https://main.php.net/note/delete/128169/bad+code
Del: spam -- https://main.php.net/note/delete/128169/spam
Del: non-english -- https://main.php.net/note/delete/128169/non-english
Del: in docs -- https://main.php.net/note/delete/128169/in+docs
Del: other reasons-- https://main.php.net/note/delete/128169
Reject -- https://main.php.net/note/reject/128169
Search -- https://main.php.net/manage/user-notes.php