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

Using SELECT * in SQL queries can lead to performance issues and potential security risks. It can retrieve unnecessary columns, leading to increased data retrieval time and network traffic. To avoid these pitfalls, it is recommended to explicitly specify the columns needed in the SELECT statement.

// Specify the columns needed in the SELECT statement
$sql = "SELECT column1, column2, column3 FROM table_name WHERE condition";
$result = mysqli_query($conn, $sql);

// Fetch and display the results
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['column1'] . ' - ' . $row['column2'] . ' - ' . $row['column3'] . '<br>';
}