Connecting to MYSQL database with PDO
Before you proceed with this tutorial ensure that your server is PDO_MYSQL driver enabled, you can find this in the php.ini file.
Created a PHP file with the name "pdo_db_conn.php" and copy the below code in it:
********************* Start copying code from here ******************************
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if($pdo){
echo "Successful";
}
}catch(PDOException $e){
echo $e->getMessage();
}
?>
********************* Code ends here *************************************
Troubleshooting
There are some common issues when you connect to a MySQL database:
If the MySQL driver is not enabled in the php.ini file, you will get the error message:
could not find driver
If you provide an incorrect password, you get the following error message:
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)
If you provide an invalid database name or the database does not exist, you get the following error message:
SQLSTATE[HY000] [1049] Unknown database 'dbname'
If you provide an invalid database hostname, the following error message will display:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
I am an experienced IT expert with 10+ years of experience with a track record of success in creating apps that are both well-received and commercially viable.
Skilled in working as a team and incorporating input into projects, a strong eye for detail, and tenacity to never quit on something until it is absolutely perfect.
Innovative use of technology for excellent delivery of tasks, ability to complete projects efficiently, and satisfy customers with attractive, user-friendly applications.
And also, the ability to oversee the development and dissemination of technology for external customers, vendors, and other clients to help improve and increase business.