Skip to content

Latest commit

 

History

History
26 lines (19 loc) · 627 Bytes

update-query-example-with-php-pdo.md

File metadata and controls

26 lines (19 loc) · 627 Bytes

UPDATE query example with PHP PDO

$st = $pdo->prepare('UPDATE test SET age = :new_age WHERE id = :id');
$st->execute([':new_age' => 53, ':id' => 1]);
  • $pdo->prepare - prepare given query to execute
  • $st->execute( - run query on the server
  • [':new_age' => 53, ':id' => 1] - bind both update values (used in SET) and filter values (used WHERE)

Example:

<?php

$pdo = new PDO('mysql:host=localhost;dbname=test', 'usr', 'pwd');

$st = $pdo->prepare('UPDATE test SET age = :new_age WHERE id = :id');
$st->execute([':new_age' => 53, ':id' => 1]);

echo $st->rowCount();
1