Skip to content

Latest commit

 

History

History
94 lines (66 loc) · 2.27 KB

Insert.md

File metadata and controls

94 lines (66 loc) · 2.27 KB

Constructor

__construct(Database $dbh, array $columns = [])

Parameter Description
$dbh PDO object for database connection
$pairs Array of key => value pairs to update

Example

use FaaPz\PDO\Database;
use FaaPz\PDO\Statement\Insert;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// INSERT INTO users (id , username , password) VALUES (? , ? , ?)
$insert = new Insert($database, ['id', 'username', 'password']);
$insert->into('users')
       ->values(1234, 'user', 'passwd');

if ($insert->execute()) {
    $insertId = $database->lastInsertId();
}

Methods

into($table)

Parameter Description
$table Table name

Example

use FaaPz\PDO\Database;
use FaaPz\PDO\Statement\Insert;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// INSERT INTO users ...
$database->insert()
         ->into('users');

columns(array $columns)

Parameter Description
$columns Array containing column names

Example

use FaaPz\PDO\Database;
use FaaPz\PDO\Statement\Insert;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
    
// INSERT INTO users (id, username, password) ...
$database->insert()
         ->into('users')
         ->columns(['id', 'username', 'password']);

values(array $values)

Parameter Description
$values Array containing column values

Example

use FaaPz\PDO\Database;
use FaaPz\PDO\Statement\Insert;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
    
// INSERT INTO users (id, username, password) VALUES (? , ? , ?), (? , ? , ?)
$insert = new Insert($database, ['id', 'username', 'password']);
$insert->into('users')
         ->values([1, 'user1', 'passwd1'])
         ->values([2, 'user2', 'passwd2']);

if (($result = $delete->execute()) !== false) {
    $affectedRows = $result->rowCount();
}