What is the potential issue with using multiple SELECT queries within a for loop in PHP?

Using multiple SELECT queries within a for loop in PHP can lead to performance issues due to the overhead of establishing a new database connection and executing the query multiple times. To solve this problem, it is recommended to retrieve all the necessary data in a single query and then loop through the results to process them.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Perform a single SELECT query to retrieve all necessary data
$sql = "SELECT column1, column2 FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Loop through the results to process them
    while($row = $result->fetch_assoc()) {
        // Process each row here
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close the database connection
$conn->close();