What are some common methods for creating a PHP interface for querying a database and retrieving information?

When querying a database in PHP, one common method is to use the PDO (PHP Data Objects) extension. PDO provides a consistent interface for accessing different types of databases, making it easier to switch between database systems. By using prepared statements with PDO, you can help prevent SQL injection attacks and improve the security of your database queries.

// Connect to the database using PDO
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    die('Connection failed: ' . $e->getMessage());
}

// Prepare and execute a query using a prepared statement
$stmt = $pdo->prepare('SELECT * FROM table WHERE column = :value');
$stmt->execute(['value' => $someValue]);

// Fetch the results as an associative array
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Loop through the results and do something with them
foreach ($results as $row) {
    echo $row['column_name'];
}