What is the best practice for querying and storing multiple values from a database in PHP?

When querying and storing multiple values from a database in PHP, it is best practice to use prepared statements to prevent SQL injection attacks and ensure data integrity. Additionally, it is recommended to fetch the results into an associative array for easier manipulation and storage.

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

// Prepare a SQL query
$stmt = $pdo->prepare('SELECT column1, column2 FROM mytable WHERE condition = :condition');

// Bind parameters
$condition = 'some_value';
$stmt->bindParam(':condition', $condition);

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

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

// Loop through the results and store them
foreach ($results as $row) {
    $value1 = $row['column1'];
    $value2 = $row['column2'];
    
    // Store or manipulate the values as needed
}