What are some common methods to retrieve data from a database in PHP?

One common method to retrieve data from a database in PHP is by using the PDO (PHP Data Objects) extension. PDO provides a consistent interface for accessing different types of databases, making it a versatile and secure option for database interactions. To retrieve data, you can use SQL queries within a PDO connection to fetch the desired information from the database.

// Establish a connection to the database using PDO
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL query to retrieve data
$stmt = $pdo->prepare('SELECT * FROM my_table WHERE column = :value');

// Bind parameter values to the query
$stmt->bindParam(':value', $someValue);

// Execute the query
$stmt->execute();

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

// Loop through the results and output the data
foreach ($results as $row) {
    echo $row['column_name'];
}