How can the results of a mysqli query be stored in a PHP array and used in a subsequent query?
To store the results of a mysqli query in a PHP array and use it in a subsequent query, you can fetch the results into an array using mysqli_fetch_all() or loop through the results and manually populate an array. Then, you can iterate over the array to access the data and use it in a subsequent query.
// Connect to the database
$conn = new mysqli('localhost', 'username', 'password', 'database');
// Query to fetch data
$query = "SELECT * FROM table";
$result = $conn->query($query);
// Store the results in an array
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
// Use the data in a subsequent query
foreach ($data as $row) {
$value = $row['column'];
$query2 = "INSERT INTO table2 (column) VALUES ('$value')";
$conn->query($query2);
}
// Close the connection
$conn->close();