Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 701 Bytes

insert-example-with-php-pdo.md

File metadata and controls

29 lines (21 loc) · 701 Bytes

Insert query example using PHP PDO

$st = $pdo->prepare('INSERT INTO test SET name = :name, age = :age');
$st->execute([':name' => 'Donald', ':age' => 90]); 
  • $pdo->prepare - prepare given query to execute
  • $st->execute( - run query on the server
  • SET name = :name, age = :age - use placeholders to insert safe values
  • [':name' => 'Donald', ':age' => 90] - values we want to insert into table

group: insert

Example:

<?php

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

$st = $pdo->prepare('INSERT INTO test SET name = :name, age = :age');
$st->execute([':name' => 'Donald', ':age' => 90]);

echo $pdo->lastInsertId();
6