Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing PDO details in php.ini

Tags:

database

php

pdo

According to the PDO documentation, you can store database login details in php.ini, away from the php file (example 3). But it does not explain how to store username and password, only the database and host. How would you do that?

This is the method used in the documentation:

[PDO]
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost"

I've tried adding the username and password to the host, but it does not like : between the username and password, and it does not recognize an address with only a username as a real address:

[PDO]
pdo.dsn.mydb="mysql:dbname=testdb;host=username:password@localhost"

I've also tried putting the username and password as separate arguments, but it does not.s

[PDO]
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost;username:username;password:secret"
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost;uid:username;pwd:secret"
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost;UID:username;PWD:secret"
like image 465
Marius Avatar asked Dec 06 '25 14:12

Marius


1 Answers

According to the documentation, this is not possible. And probably not wanted. If you look at the PDO __construct() method, it takes 3 arguments: a DSN (which can be a name to a pdo.dsn.name defined in your php.ini), a username and a password.

like image 51
Percutio Avatar answered Dec 08 '25 04:12

Percutio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!