What are the potential pitfalls of not properly defining and using PDO connections in PHP?

Potential pitfalls of not properly defining and using PDO connections in PHP include security vulnerabilities such as SQL injection attacks, inefficient database queries, and difficulty in maintaining and scaling the application. To avoid these issues, it is important to always use prepared statements, properly handle exceptions, and close the connection after use.

// Define PDO connection
try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Error connecting to database: " . $e->getMessage());
}

// Use prepared statements for secure queries
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();

// Close connection after use
$pdo = null;