Why PDO constructor does not accept host, dbname, database type, etc. As arguments to a regular function?

I am learning PDO, and the constructor seems to use an unorthodox and inconsistent way of accepting arguments. Namely:

$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 

Why is it different from the usual format:

 $dbh = new PDO("mysql", $host, $dbname, $user, $pass); 

Or, since the first two arguments (host and dbname) are written as one long string, why not continue with the other two arguments? Namely:

 $dbh = new PDO("mysql:host=$host;dbname=$dbname;user=$user;pass=$pass"); 
+4
source share
2 answers

The DSN describes where the data source is and how to connect to it. User and password are authentication parameters that do not affect access to the data source.

Having a user and a pass in the DSN will look like you have to enter your username and password in the URL of the website you are going to.

+4
source

PDO uses DSN to connect, username and password are optional parameters for DSN string.

check out http://www.php.net/manual/en/pdo.construct.php

I really don't think this is β€œunorthodox or inconsistent," ODBC in PHP uses the same process.

0
source

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


All Articles