How can one effectively utilize both PHP and MySQL in web development projects?

To effectively utilize both PHP and MySQL in web development projects, one can establish a connection to the MySQL database using PHP, execute queries to retrieve or manipulate data, and then process the results accordingly in the PHP code.

// Establishing a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

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

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

// Executing a query to retrieve data
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Processing the results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

// Closing the connection
$conn->close();