how to fetch and update data from database in php?

Today, We want to share with you update query in mysql php.In this post we will show you delete query in php, hear for Update Data In a MySQL Table Using MySQLi and PDO we will give you demo and example for implement.In this post, we will learn about PHP Crud Mysqli Select Insert Update And Delete Query with an example.

how to fetch and update data from database in php?

To specify which data records we want to update query, you can use where after select query and update statements in MySQL.

Syntax

UPDATE `table_name` SET `column_name` = `new_value' [WHERE condition];

Example (MySQLi Object-oriented)

<?php
$servername = "localhost";
$username = "tamilrokers";
$password = "Abc***989865";
$dbname = "members";

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

if ($link->connect_error) {
  die("Connection failed: " . $link->connect_error);
}

$sql = "UPDATE allMembers SET lastname='Kamlesh' WHERE id=2";

if ($link->query($sql) === TRUE) {
  echo "Member updated successfully";
} else {
  echo "Error updating record: " . $link->error;
}

$link->close();
?>

Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "tamilrokers";
$password = "Abc***989865";
$dbname = "kochi";

$link = mysqli_connect($servername, $username, $password, $dbname);
if (!$link) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE allMembers SET lastname='rakhisaban' WHERE id=2";

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

mysqli_close($link);
?>

Example (PDO)

<?php
$servername = "localhost";
$username = "tamilrokers";
$password = "Abc***989865";
$dbname = "dharmsala";

try {
  $link = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

  $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $sql = "UPDATE allMembers SET lastname='Bhakti' WHERE id=2";

  $stmt = $link->prepare($sql);

  $stmt->execute();

  echo $stmt->rowCount() . " records UPDATED successfully";
} catch(PDOException $e) {
  echo $sql . "<br>" . $e->getMessage();
}

$link = null;
?>

Update query PHP MySQL

mysql_query("UPDATE articles SET content = 'here all about my system', title = 'this is a sample text' WHERE id = $id");

MySQL UPDATE column

UPDATE members SET receive_qty=20;

MySQL UPDATE with WHERE

UPDATE members 
SET receive_qty=25 
WHERE current_level>50;

MySQL UPDATE using NULL

UPDATE members 	
SET receive_qty=20,pub_lang='Hindi',pub_lang=NULL 
WHERE current_level>50;

MySQL UPDATE multiple columns

UPDATE members 
SET receive_qty=20,pub_lang='Hindi',receive_dt='2008-07-10' 
WHERE current_level>50;

MySQL UPDATE with subqueries

UPDATE  members 
SET current_level=current_level*.05
WHERE cate_id IN(SELECT cate_id 
FROM mailorders 
WHERE receive_qty>10);

Examples: MySQL UPDATE on multiple tables

UPDATE news_mast,mailorders
SET news_mast.news_price=news_mast.news_price+(news_mast.news_price*.05),
mailorders.current_level=mailorders.current_level+(mailorders.current_level*.05),
mailorders.total_cost=receive_qty*(mailorders.current_level+(mailorders.current_level*.05))
WHERE news_mast.news_id=mailorders.news_id
AND mailorders.pub_lang="English";

I hope you get an idea about update query in mysql php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.