Change language

PHP MySQL UPDATE Query

The MySQL UPDATEquery is used to update existing records in a table in a MySQL database.
  • It can be used to update one or more fields at the same time.
  • It can be used to specify any condition with a WHERE clause.
  • Syntax:
    Basic syntax for update request - Implementing an update request where: Let’s take a look at the following table "Data" with four columns "ID", "FirstName", "LastName" and "Age". To update the Age of the person whose ID is 201 in the Data ", We can use the following code: Refresh query using procedural method: php $ link = mysqli_connect ("localhost", "root", "", "Mydb");  
    if ($link === false) { die ("ERROR: Could not connect." . mysqli_connect_error()); } $ sql = "UPDATE data SET Age = ’28’ WHERE id = 201" ; if (mysqli_query ($link, $sql)) { echo "Record was updated successfully."; } else { echo "ERROR: Could not be able to execute $sql." . mysqli_error ($link); }
    mysqli_close ($link);
    ?>
    Output: Table after update - Web browser output: Refresh the request using the object-oriented method: php $ mysqli = new mysqli ("localhost", "root", " "," Mydb ");  
    if ($mysqli === false) { die ("ERROR: Could not connect." . $mysqli-> connect_error); }  
    $sql = "UPDATE data SET Age =’ 28’ WHERE id = 201 ";
    if ($mysqli-> query ($sql) === true) { echo" Records was updated successfully. "; } else { echo "ERROR: Could not be able to execute $sql." . $mysqli-> error; }
    $mysqli-> close();
    ?>
    Output: Table after update - Web browser output: Refresh request using PDO method: php try { $ pdo = new PDO (" mysql: host = localhost ; dbname = Mydb "," root "," "); $pdo-> setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die ("ERROR: Could not connect." . $e-> getMessage()); }  
    try { $sql =" UPDATE data SET Age = ’28’ WHERE id = 201"; $pdo-> exec ($sql); echo "Records was updated successfully."; } catch (PDOException $e) { die ("ERROR: Could not be able to execute $sql." . $e-> getMessage()); }
    unset ($pdo);
    ?>
    Output: Table after update - Web browser output:

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically