What are common pitfalls in PHP scripting that can lead to slow loading times?

One common pitfall in PHP scripting that can lead to slow loading times is inefficient database queries. To solve this issue, make sure to optimize your queries by using indexes, limiting the amount of data retrieved, and avoiding unnecessary joins.

// Example of optimizing a database query by adding indexes
// Before optimization
$query = "SELECT * FROM users WHERE username = 'john'";
$result = mysqli_query($connection, $query);

// After optimization
$query = "SELECT * FROM users WHERE username = 'john'";
// Add an index on the username column for faster retrieval
$index_query = "CREATE INDEX idx_username ON users (username)";
mysqli_query($connection, $index_query);
$result = mysqli_query($connection, $query);