What are the best practices for selecting specific columns in a MySQL query in PHP to optimize performance?

When selecting specific columns in a MySQL query in PHP, it is important to only retrieve the columns that are needed to minimize the amount of data being transferred. This can improve query performance by reducing the amount of data that needs to be processed. Additionally, using indexes on the selected columns can further optimize the query execution.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Select specific columns and use indexes
$sql = "SELECT column1, column2 FROM myTable WHERE condition = 'value'";
$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"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();