Junior — Middle
Have you had experience using PHP Data Objects (PDO) for working with databases?
sobes.tech AI
Answer from AI
PHP Data Objects (PDO) is a PHP extension for accessing databases through a unified interface. It supports many DBMSs (MySQL, PostgreSQL, SQLite, etc.) and allows writing safer and more portable code.
Main advantages of PDO:
- Support for prepared statements, which helps prevent SQL injection.
- A unified interface for different databases.
- Ability to switch between drivers without changing the main code.
Example of using PDO for connection and data retrieval:
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->execute(['id' => 1]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($user);
If you've worked with PDO, it means you've used these features for safe and convenient database interaction.