Tags
Asked 3 years ago
17 Jun 2021
Views 227
Toney

Toney posted

update MySQL in PHP

update MySQL in PHP
denyy

denyy
answered Apr 28 '23 00:00

To update a MySQL database in PHP, you can use the mysqli_query() function. Here's an example code snippet:


<?php
// Connect to the database
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabasename";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

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

// Update data in the database
$sql = "UPDATE yourtable SET yourcolumn='newvalue' WHERE id=1";

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

// Close the database connection
mysqli_close($conn);
?>

In the above code, we first connect to the database using mysqli_connect (). Then we execute an SQL UPDATE statement using mysqli_query (). If the query is successful, we print a success message; otherwise, we print an error message.

Note that you need to replac e yourusername, yourpassword, yourdatabasename, yourtable, yourcolumn, newvalue , and id with your own values. Also, don't forget to close the database connection using mysqli_close () after you're done with it.
Post Answer