How can SQL queries be integrated into PHP code to store data retrieved from arrays in a database efficiently and effectively?
To store data retrieved from arrays in a database efficiently and effectively using SQL queries in PHP, you can iterate through the array and execute INSERT queries for each row of data. This allows you to insert multiple rows of data into the database in a single operation, reducing the number of database calls and improving performance.
// Assuming $data is the array containing the data to be stored in the database
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
// Prepare the SQL query
$stmt = $pdo->prepare("INSERT INTO table_name (column1, column2) VALUES (:value1, :value2)");
// Iterate through the array and insert data into the database
foreach ($data as $row) {
$stmt->bindParam(':value1', $row['value1']);
$stmt->bindParam(':value2', $row['value2']);
$stmt->execute();
}