This repository was archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Database
Nick Barham edited this page Jun 1, 2017
·
4 revisions
use Automatorm\DataLayer\Database\Connection;
// Get db connection (registered as 'default')
$connection = Connection::register([
'server' => 'database_url/ipaddress',
'user' => 'root',
'password' => 'password',
'database' => 'defaultschema'
]);
// Alternatively, use and existing PDO connection
$connection = Connection::register($pdo);Having set up a database connection above, we can then run queries:
use Automatorm\Database\Query;
// Create a new query, assuming default connection 'default'
$query = new Query($connection);
// Specify a query to run
$query->sql('SELECT * FROM blog WHERE id = 1');
// Run query, and get results.
$results = $query->execute();->execute() returns a 3 level array, corresponding to query, row and columns. Therefore, in the example above, which would return a single row, we can access the data thus:
echo $results[0][0]['title']; // Column 'title', from row 0, from query 0For safety, we probably want to use parameterised queries to pass that '1' into the query. This can be done by passing an array of data as a second parameter to ->sql() like this:
// Pass data through as an array of values, instead of inline
$query->sql('SELECT * FROM blog WHERE id = ?', [1]);