Question
Get detailed answers from our community
How to connect to MySQL database in PHP?
Database
Asked Aug 21, 2025
by
admin
1
AnswerAnswered
I am trying to connect to a MySQL database using PHP PDO but getting connection errors. Can someone help me with the correct syntax?
Answer
You can use PDO like this:
$pdo = new PDO('mysql:host=localhost;dbname=your_db', $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Make sure your credentials are correct!
$pdo = new PDO('mysql:host=localhost;dbname=your_db', $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Make sure your credentials are correct!