PHP
May 10, 2026
How to Connect PHP to a Remote MySQL Database
PHP and MySQL are the foundation of the modern web. Whether you are using a framework like Laravel or writing raw PHP, connecting to a remote FreeDB instance is simple and efficient.
1. Using PDO (Recommended)
PHP Data Objects (PDO) is the modern, secure way to handle database connections. It supports prepared statements to prevent SQL injection.
<?php
$host = 'mysql.freedb.tech';
$db = 'freedb_your_database_name';
$user = 'freedb_your_username';
$pass = 'your_password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
echo "Connected to FreeDB successfully!";
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
2. Using MySQLi
If you prefer the MySQL Improved extension, you can use this procedural or object-oriented approach:
<?php
$servername = "sql.freedb.tech";
$username = "freedb_your_username";
$password = "your_password";
$dbname = "freedb_your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to FreeDB";
?>
Which one should I use?
Always prefer PDO for new projects. It is more flexible (supporting multiple database types) and encourages better security practices like prepared statements. MySQLi is excellent for legacy support or highly specific MySQL features.