Sobes.tech
Junior — Middle

What methods do you use to extract data from a database?

sobes.tech AI

Answer from AI

To extract data from a database in PHP, the following methods are commonly used:

  1. PDO (PHP Data Objects) — a universal and secure way to work with databases. It allows the use of prepared statements, which protect against SQL injection.
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->execute(['id' => $userId]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
  1. MySQLi — an extension for working with MySQL, supporting both procedural and object-oriented styles.

  2. ORM (Object-Relational Mapping) — for example, Doctrine or Eloquent (Laravel), which allow working with the database through objects, simplifying code and increasing readability.

The choice of method depends on the project and requirements for security, performance, and ease of maintenance.