What are the potential pitfalls of using 'SELECT *' in SQL queries and how can they be mitigated?

Using 'SELECT *' in SQL queries can lead to performance issues and potential security vulnerabilities. It can retrieve unnecessary columns, leading to increased data transfer and processing time. To mitigate this, explicitly specify the columns needed in the SELECT statement.

// Specify the columns needed in the SELECT statement instead of using 'SELECT *'
$sql = "SELECT column1, column2, column3 FROM table_name WHERE condition";
$result = mysqli_query($connection, $sql);

// Process the query result
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process the data
    }
}