Database Connection With PDO
Learn how to connect a PHP script to MySQL, using the PDO extension.
We'll cover the following...
PDO (PHP Data Objects) is an extension that enables connection to any SQL database.
Let’s see how we can use this extension.
At the root our project, we’ll create a new file and call it install.php
. Inside the file, we’ll create a new PDO()
and store it under the $connection
variable.
$connection = new PDO(params);
The parameters required for this process are:
- DSN (data source name) that defines database type, host name, database name (optional)
- The DSN (data source name), which defines the database type, host name, and database name (optional)
- Username
- Password
- Additional options, such as encoding and error
Let’s see this in practice:
...