What is the purpose of using LIMIT in a MySQL query when dealing with arrays in PHP?

When dealing with arrays in PHP and querying a MySQL database, using LIMIT in the SQL query can help to control the number of results returned, which can be useful for pagination or limiting the amount of data processed by the PHP script. This can improve the performance of the script and prevent memory issues when dealing with large datasets.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Query with LIMIT to retrieve only 10 results
$query = "SELECT * FROM table_name LIMIT 10";
$result = $mysqli->query($query);

// Fetch and process the results
while ($row = $result->fetch_assoc()) {
    // Process each row
}

// Close database connection
$mysqli->close();