What is the potential issue with using SELECT * in a SQL query?

Using SELECT * in a SQL query can potentially retrieve more data than needed, leading to increased network traffic, slower query execution, and potential security risks if sensitive data is inadvertently exposed. To solve this issue, explicitly specify the columns needed in the SELECT statement to retrieve only the necessary data.

<?php
// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Specify the columns needed in the SELECT statement
$stmt = $pdo->prepare("SELECT column1, column2, column3 FROM mytable");

// Execute the query
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Output the results
foreach ($results as $row) {
    echo $row['column1'] . ' ' . $row['column2'] . ' ' . $row['column3'] . '<br>';
}
?>