What are some best practices for selecting specific columns from a database table in a MySQL query when using PHP?

When selecting specific columns from a database table in a MySQL query using PHP, it is best practice to explicitly list the columns you want to retrieve rather than using '*' to select all columns. This can improve query performance by reducing the amount of data transferred from the database server to the PHP script.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Select specific columns from the table
$sql = "SELECT column1, column2, column3 FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. " - Column3: " . $row["column3"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>