Saving PDO information in php.ini

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

This is the method used in the documentation:

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

I tried adding a username and password to the host, but he doesn’t like it: between the username and password and does not recognize the address with only the username as the real address:

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

I also tried putting the username and password as separate arguments, but that 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"
+3
source share
2 answers

. , , . PDO __construct(), 3 : DSN ( pdo.dsn.name, php.ini), .

+1

, PHP script $_ENV.

AFIACT, php.ini, apache, .htaccess. .

SetEnv mysql_username dbuser
SetEnv mysql_password dbpass

PHP :

$db = new PDO('mydb',$_ENV['mysql_username'],$_ENV['mysql_password']);

DSN , .

0

Source: https://habr.com/ru/post/1721497/


All Articles