Before using database we need to connect it first. This process is common in all types of database irrespective of language we use. We will learn here the code for connecting to MySQL database using PDO class.

<?php
// Database Connection Configuration
// Use either MySQL or SQLite by uncommenting the respective section
$host_name = "localhost";
$database = "pdo"; // Change this to your actual database name
$username = ""; // Your MySQL database username
$password = ""; // Your MySQL database password
//////// Establish Connection to MySQL Database ////////
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print("Error!: " . $e->getMessage() . "<br/>");
die();
}
///// For SQLite Database Connection /////
// Uncomment the line below to switch to SQLite
// $dbo = new PDO('sqlite:' . dirname(__FILE__) . '/my_student.db');
?>
More on SQLite connection
require "config.php";
Connecting to a PostgreSQL database with PDO is similar to MySQL but requires a different DSN. Here’s an example:
<?php
try {
$dbo = new PDO('pgsql:host=localhost;dbname=testdb', 'username', 'password');
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connection successful!";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
It’s possible to define a connection timeout by setting the ATTR_TIMEOUT attribute:
<?php
$dbo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dbo->setAttribute(PDO::ATTR_TIMEOUT, 5); // Timeout set to 5 seconds
?>
Persistent connections improve performance by reusing the same connection rather than opening a new one each time:
<?php
$dbo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password', array(
PDO::ATTR_PERSISTENT => true
));
?>
Handling connection errors in a structured way helps improve user experience and debugging:
<?php
try {
$dbo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connection successful!";
} catch (PDOException $e) {
error_log("Connection failed: " . $e->getMessage()); // Log the error
echo "Database connection could not be established.";
}
?>
<?Php
$host_name = "34.68.103.244";
$database = "my_tutorial"; // Change your database name
$username = "root-plus2net"; // Your database user id
$password = "************"; // Your password
//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
/// Sample script to display records ////
$sql="select * from student ";
echo "<table>";
foreach ($dbo->query($sql) as $row) {
echo "<tr ><td>$row[name]</td></tr>";
}
echo "</table>";
?>
Details of MySQL database setupDownload Zip file to test your PHP PDO script
PDO References PDO Fetch record
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
02-10-2021 | |
| cant we connect to a phpmyadmin sql database? | |