Sobes.tech
Junior — Middle

Did you use only PostgreSQL when creating database queries?

sobes.tech AI

Answer from AI

Yes, when working with databases in PHP, PostgreSQL is often used, especially for projects that require reliability and advanced SQL capabilities.

To interact with PostgreSQL in PHP, extensions such as:

  • PDO (PHP Data Objects) with the pgsql driver — a universal interface for working with different DBMS.
  • pg_connect and other functions of the pgsql extension — specific to PostgreSQL.

An example of a simple query using PDO:

$dsn = 'pgsql:host=localhost;port=5432;dbname=testdb;';
$user = 'user';
$password = 'password';

try {
    $pdo = new PDO($dsn, $user, $password);
    $stmt = $pdo->query('SELECT * FROM users');
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        print_r($row);
    }
} catch (PDOException $e) {
    echo 'Connection error: ' . $e->getMessage();
}

Thus, PostgreSQL is a popular choice, and it is convenient to work with in PHP thanks to support through PDO and the pgsql extension.