// Connect to database try { $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); } catch (PDOException $e) { echo "Database connection failed: " . $e->getMessage(); } // Execute query $stmt = $pdo->prepare('SELECT * FROM users'); if ($stmt->execute()) { // Fetch results $rows = $stmt->fetchAll(); print_r($rows); } else { // Handle error $error = $stmt->errorCode(); echo "Query failed with error code: $error"; }In the above example, we first establish a database connection using the PDO constructor. We then prepare a SELECT query and attempt to execute it using the execute() method. If the query succeeds, we fetch the results using fetchAll() and print them using print_r(). However, if the query fails, we use errorCode() to retrieve the error code and display it to the user. Package library: PDO is bundled with the PHP core, so no additional package needs to be installed.