MYSQL PDO Connection: PDO tutorial for MSYQL Developer
what is PDO?
“PHP Data Objects (PDO) is a database abstraction layer giving a uniform technique for access to different databases.”
Numerous PHP developers figured out how to get to databases by utilizing either the MySQL or MySQLi expansions. As of PHP 5.1, there’s a superior way. PHP Data Objects (PDO) give strategies to prepared statements and working with objects that will make you significantly more gainful!
MYSQL PDO Connection
Connecting MYSQL using old way:
$Con = mysql_connect(‘servername’, ‘username’, ‘userpassword’);
Connecting MYSQL using new way:
Create a new PDO object. PDO’s constructor takes 4 parameters, DSN, username, password, and an array of driver options.
A DSN is fundamentally a string of options that advise PDO which driver to utilize, and the connection points of interest. You can look up every one of the options here PDO MYSQL DSN.
$db = new PDO(‘mysql:host=localhost;dbname=testdb;charset=utf8’, ‘username’, ‘password’);
Note: On the off chance that you get an error about character sets, ensure you add the charset parameter to the DSN. Adding the charset to the DSN is essential for security reasons most cases you’ll see around forget it. So you have to include the DSN.