How can PHP scripts be optimized to prevent server crashes when performing searches on a MySQL database?

To prevent server crashes when performing searches on a MySQL database, PHP scripts can be optimized by limiting the number of results returned, optimizing database queries, and using pagination to split large result sets into smaller chunks.

// Limit the number of results returned
$query = "SELECT * FROM table LIMIT 100";

// Optimize database queries
$query = "SELECT column1, column2 FROM table WHERE condition = 'value'";

// Implement pagination
$limit = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM table LIMIT $offset, $limit";