1) Write the regular update statement and again, instead of values, assign the named placeholders. For example:
[code language=”php”]
$sql = "UPDATE `users`
SET `city`= :city, `phone` = :tel
WHERE `id` = :id";
[/code]
2) Prepare the query:
[code language=”php”]
$query = $dbh->prepare($sql);
[/code]
3) Bind the parameters:
[code language=”php”]
$query -> bindParam(‘:city’, $city, PDO::PARAM_STR);
$query -> bindParam(‘:tel’ , $tel , PDO::PARAM_INT);
$query -> bindParam(‘:id’ , $id , PDO::PARAM_INT);
[/code]
4) Define the bound values:
[code language=”php”]
$tel = ‘3456789012’;
$city = ‘Bokaro’;
$id = 1;
[/code]
5) Execute the query:
[code language=”php”]
$query -> execute();
[/code]
6) Check that the query has been performed and that the database has been successfully updated.
[code language=”php”]
if($query -> rowCount() > 0)
{
$count = $query -> rowCount();
echo $count . " rows were affected.";
}
else
{
echo "No affected rows.";
}
All together now:
$sql = "UPDATE users
SET `city`= :city, `phone` = :tel
WHERE `id` = :id";
$query = $dbh->prepare($sql);
$query -> bindParam(‘:city’, $city, PDO::PARAM_STR);
$query -> bindParam(‘:tel’ , $tel , PDO::PARAM_INT);
$query -> bindParam(‘:id’ , $id , PDO::PARAM_INT);
$tel = ‘11223456789’;
$city = ‘Pune’;
$id = 1;
$query -> execute();
if($query -> rowCount() > 0)
{
$count = $query -> rowCount();
echo $count . " rows were affected.";
}
else
{
echo "No affected rows.";
}
[/code]
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND