Tutorialsplane

PHP Update Data


PHP Update Data:– Mysql UPDATE Clause is used to modify records of a table at any time where you want by using WHERE clause.Following example shows how we changed the lastname of “John Doe” to “John Henry

Example // How to use Limit Clause to fetch data

<?php $servername = "localhost";
$username = "root";
$password = "";
$dbname = "employee";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn-?>connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "UPDATE JohnData SET lastname='Henry' WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
	echo "<br/>";
} else {
    echo "Error updating record: " . $conn->error;
}
$sql = "SELECT id, firstname, lastname FROM JohnData";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br/>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

PHP Tutorial