PHP code example of scottchiefbaker / dbquery

1. Go to this page and download the library: Download scottchiefbaker/dbquery library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

scottchiefbaker / dbquery example snippets




// SQLite
$dsn = "sqlite://path/to/dir/database.sqlite";
$dbq = new DBQuery($dsn);

// MySQL
$dsn  = 'mysql:host=server.domain.com;dbname=my_database';
$user = 'john_smith';
$pass = 'sekrit';
$dbq  = new DBQuery($dsn, $user, $pass);

$sql  = "SELECT First, Last, City, State, Zipcode FROM Customers;";
$data = $dbq->query($sql);

foreach ($data as $rec) {
	// Output code here
}

$result = $dbq->query($sql);
$result = $dbq->query($sql, $return_hint);
$result = $dbq->query($sql, $param_array, $return_hint);

$sql  = "SELECT First, Last, City FROM Customers;";
$data = $dbq->query($sql, 'info_hash');

foreach ($data as $i) {
	print "Cust: " . $i['First'] . " " . $i['Last'];
}

$sql  = "SELECT First, Last, City FROM Customers;";
$data = $dbq->query($sql, 'info_list');

foreach ($data as $i) {
	print "Cust: " . $i[0] . " " . $i[1];
}

$sql = "SELECT CustID FROM Customers WHERE Last = 'Doolis';";
$id  = $dbq->query($sql, 'one_data');

$sql  = "SELECT ID, Last FROM Customers;";
$data = $dbq->query($sql, 'key_value');

print "Customer #17 = " . $data[17];
print "Customer #21 = " . $data[21];

$sql  = "SELECT First, Last FROM Customers WHERE City = 'Chicago';";
$data = $dbq->query($sql, 'one_row');

print "Customer: " . $data['First'] . " " . $data['Last'];

$sql = "SELECT ID FROM Customers WHERE City = 'Chicago';";
$ids = $dbq->query($sql, 'one_column');

print "Found IDs: " . join(", ", $ids);

$sql    = "INSERT INTO Names (First, Last, Age) VALUES (?, ?, ?);";
$params = array("Jason", "Doolis", 14);

$id = $dbq->query($sql, $params);

php tests/unit_test.php