What are some best practices for setting limits on database queries in PHP to improve performance?
When querying a database in PHP, it is important to set limits on the number of results returned to improve performance. This can prevent excessive amounts of data from being retrieved and processed, leading to faster query execution times. One way to achieve this is by using the LIMIT clause in SQL queries to restrict the number of rows returned.
// Example of setting a limit on database queries in PHP
$query = "SELECT * FROM table_name LIMIT 10";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
// Process each row
}
} else {
echo "No results found.";
}