In what ways can PHP and MySQL interact to optimize memory usage and prevent server memory overflow during query execution?

To optimize memory usage and prevent server memory overflow during query execution, you can use PHP's `mysqli` extension to fetch rows from a MySQL query one at a time rather than loading the entire result set into memory at once. This allows you to process large datasets without consuming excessive memory.

// Establish a connection to the MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Run a query and fetch rows one at a time
$result = $mysqli->query("SELECT * FROM table");
while ($row = $result->fetch_assoc()) {
    // Process each row here
}

// Close the result set and the database connection
$result->close();
$mysqli->close();