What are the basic steps to access specific fields in a MySQL table using PHP?

When accessing specific fields in a MySQL table using PHP, you need to establish a connection to the database, query the table to retrieve the desired fields, and then fetch the results to access the specific data. This can be achieved by using the mysqli extension in PHP.

<?php

// Establish a connection to the MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database_name");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Query the table to retrieve specific fields
$sql = "SELECT field1, field2 FROM table_name";
$result = mysqli_query($connection, $sql);

// Fetch the results and access the specific data
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Field 1: " . $row["field1"] . "<br>";
        echo "Field 2: " . $row["field2"] . "<br>";
    }
} else {
    echo "0 results";
}

// Close the connection
mysqli_close($connection);

?>