// Create a new PDO connection $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); // Get the current connection's database name $dbName = $pdo->getAttribute(PDO::ATTR_DATABASE_NAME); // Output the database name echo "The current database name is: " . $dbName;
// Create a new PDO connection $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); // Set the error mode to exceptions $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Insert a new record into a table $stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (?, ?)"); $stmt->execute(['John Doe', 'johndoe@example.com']);Example 2 above sets the error mode to `PDO::ERRMODE_EXCEPTION` to throw an exception when an error occurs. It then prepares and executes an SQL statement to insert a new record into a database table. The package library that the PDO class belongs to is the PHP Data Objects (PDO) extension, which is included in the core PHP package.