How can you properly update a column in a MySQL database using PHP?

To properly update a column in a MySQL database using PHP, you can use the SQL UPDATE statement along with PHP's MySQLi functions. First, establish a connection to the database, then execute the UPDATE query with the new column value and a condition to specify which rows to update.

<?php
// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if($connection === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Update a column in the database
$newValue = "new value";
$id = 1; // Example condition
$sql = "UPDATE table_name SET column_name = '$newValue' WHERE id = $id";

if(mysqli_query($connection, $sql)){
    echo "Column updated successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($connection);
}

// Close connection
mysqli_close($connection);
?>