Useful code examples and solutions I've collected. Feel free to use and learn from them!
// PHP Database Connection
// Secure database connection using PDO
<?php
try {
$pdo = new PDO(
"mysql:host=localhost;dbname=mydb",
"username",
"password"
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>