What is the correct way to store database content in a variable in PHP?

When storing database content in a variable in PHP, it is important to properly handle the database connection, query the database, fetch the results, and store them in a variable. This can be achieved by using PHP's PDO (PHP Data Objects) or MySQLi extension to connect to the database and execute the query. Once the data is fetched, it can be stored in a variable for further processing.

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

// Prepare and execute a query
$stmt = $pdo->query("SELECT * FROM my_table");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Store the fetched results in a variable
$databaseContent = $results;