What are the best practices for selecting columns in a SQL query instead of using "SELECT *"?

Selecting specific columns in a SQL query instead of using "SELECT *" is considered a best practice because it can improve query performance by reducing the amount of data fetched from the database. It also makes the query more readable and maintainable by explicitly stating which columns are being retrieved. To select specific columns, simply list the column names after the SELECT keyword in the query.

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

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

// Fetch and display the results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column 1: " . $row["column1"]. " - Column 2: " . $row["column2"]. " - Column 3: " . $row["column3"]. "<br>";
    }
} else {
    echo "0 results";
}

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