[TODO]
$this->db = new \WebServCo\Framework\Libraries\MysqlPdoDatabase(
[
'connection' => [
'host' => '',
'username' => '',
'passwd' => '',
'dbname' => '',
'port' => '',
],
]
);-
This method uses a transaction.
-
Returns lastInsertId regardless of queries executed as it's not possible to retrieve lastInsertId after commit
$lastInsertId = $this->db->insert('<tableName>', ['<col1>' => <val1>, '<col2>' => <val2>]);-
This method uses a transaction.
-
Not supported when adding multiple rows at once
$this->db->insert('<tableName>', [<addData>], [<updateData>]);
$this->db->insert(
'<tableName>',
['<col1>' => <val1>, '<col2>' => <val2>],
['<col2>' => <val2>]
);-
This method uses a transaction.
-
Returns lastInsertId regardless of queries executed as it's not possible to retrieve lastInsertId after commit
$lastInsertId = $this->db->insertIgnore('<tableName>', ['<col1>' => <val1>, '<col2>' => <val2>]);-
This method uses a transaction.
-
Returns lastInsertId regardless of queries executed as it's not possible to retrieve lastInsertId after commit
$lastInsertId = $this->db->replace('<tableName>', ['<col1>' => <val1>, '<col2>' => <val2>]);- MySQL: Please note the information about the last inserted Id below
$this->db->insert(
'<tableName>',
[
['<col1>' => <val1>, '<col2>' => <val2>],
['<col1>' => <val3>, '<col2>' => <val4>],
]
);return $this->db->getRows("SELECT <col1>, <col2> FROM <table> WHERE 1", []);return $this->db->getRow("SELECT <col1>, <col2> FROM <table> WHERE <col3> = ?", ['<val3>']);return $this->db->getColumn(
"SELECT <col1>, <col2> FROM <table> WHERE <col3> = ?", // query
['<val3>'], // params
0 // column number
);$data = [];
$stmt = $this->db->query("SELECT <col1>, <col2> FROM <table> WHERE <col3> = ?", ['<val3>']);
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$data[] = $row;
}$this->db->query("DELETE FROM <table> WHERE <col> = ? LIMIT 1", [<val>]);
return $this->db->affectedRows();$this->db->query("UPDATE <table> SET <col1> = ? WHERE <col2> = ?", ['<val1>', '<val2>']);
return $this->db->affectedRows();$this->db->transaction(
[
["INSERT INTO <mainTable> (<col1>, <col2>) VALUES (?, ?)", [<val1>, <val2>]],
["SET @id = LAST_INSERT_ID();", []],
["INSERT INTO <secondaryTable> (id, <col1>, <col2>) VALUES (@id, ?, ?)", [<val1>, <val2>]],
["INSERT INTO <secondaryTable> (id, <col1>, <col2>) VALUES (@id, ?, ?)", [<val1>, <val2>]],
]
);
return $this->db->getColumn("SELECT @id", [], 0);If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.
- Important: When using transactions, this method is not useful as it returns
0. If using the insert helpers,lastInsertIdis returned by those methods.
return $this->db->lastInsertId();return $this->db->affectedRows();- It is strongly recommended to avoid this functionality, as it adds an usually unnecessary overhead.
- Alternative: use
$this->db->getRows()and count the result.
$this->db->query("SELECT <col> FROM <table>");
return $this->db->numRows();return $this->db->valueExists('<table>', '<col>', '<val>');return $this->db->tableExists('<table>');Please see DatabaseException.
$db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$stmt = $db->query(...);
$db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
while (...)Generate placeholders string from array data.
Eg. if array has 3 items, it generates the string "?, ?, ?".