PHP code example of toropyga / db

1. Go to this page and download the library: Download toropyga/db 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/ */

    

toropyga / db example snippets


const DB_PGSQL_HOST = '127.0.0.1';     // PostgreSQL server name or address
const DB_PGSQL_PORT = 5432;            // PostgreSQL server port
const DB_PGSQL_NAME = 'database';      // Database name
const DB_PGSQL_USER = 'user';          // User name
const DB_PGSQL_PASS = 'password';      // User password
const DB_PGSQL_STORAGE = true;         // Keep connection for the session
const DB_PGSQL_DEBUG = false;          // Enable or disable debugging
const DB_PGSQL_ERROR_EXIT = false;     // Throw DatabaseException on errors
const DB_PGSQL_LOG_NAME = 'db.log';    // Log file name
const DB_PGSQL_LOG_ALL = true;         // Log all actions or only errors

const DB_MYSQL_HOST = '127.0.0.1';     // MySQL server name or address
const DB_MYSQL_PORT = 3306;            // MySQL server port
const DB_MYSQL_NAME = 'database';      // DB name
const DB_MYSQL_USER = 'user';          // User name
const DB_MYSQL_PASS = 'password';      // User password
const DB_MYSQL_STORAGE = true;         // Maintain connection for entire session
const DB_MYSQL_USE_TRANSACTION = true; // Use transaction
const DB_MYSQL_DEBUG = false;          // Enable or disable debugging features
const DB_MYSQL_ERROR_EXIT = false;     // Throw DatabaseException if an error occurs
const DB_MYSQL_LOG_NAME = 'db.log';    // Log file name
const DB_MYSQL_LOG_ALL = true;         // Log all actions (true) or only errors (false)

const DB_ORACLE_HOST = 'db.example';   // Oracle server name or address
const DB_ORACLE_PORT = 1521;           // Oracle server port
const DB_ORACLE_NAME = 'service';      // DB name
const DB_ORACLE_USER = 'user';         // User name
const DB_ORACLE_PASS = 'password';     // User password
const DB_ORACLE_STORAGE = true;        // Maintain connection for entire session
const DB_ORACLE_CHARSET = 'AL32UTF8';  // Charset
const DB_ORACLE_DEBUG = false;         // Enable or disable debugging features
const DB_ORACLE_ERROR_EXIT = false;    // Throw DatabaseException if an error occurs
const DB_ORACLE_LOG_NAME = 'db.log';   // Log file name
const DB_ORACLE_LOG_ALL = true;        // Log all actions (true) or only errors (false)
const DB_ORACLE_USE_HOST = 2;          // Connection record type:
                                       //    0 - only the DB name is used
                                       //    1 - host and DB name is used
                                       //    2 - full entry is used for connection

const DB_PDO_TYPE = 'mysql';           // DB type ['mysql', 'pgsql', 'oci', 'odbc', 'sqlite', 'sqlsrv', 'dblib', 'firebird']
const DB_PDO_HOST = '127.0.0.1';       // DB server name or address
const DB_PDO_PORT = 3306;              // DB server port
const DB_PDO_NAME = 'database';        // DB name
const DB_PDO_USER = 'user';            // User name
const DB_PDO_PASS = 'password';        // User password
const DB_PDO_DEBUG = false;            // Enable or disable debugging features
const DB_PDO_ERROR_EXIT = false;       // Throw DatabaseException if an error occurs
const DB_PDO_ORACLE_CONNECT_TYPE = 2;  // Oracle connection record type:
                                       //    0 - only the DB name is used
                                       //    1 - host and DB name is used
                                       //    2 - full entry is used for connection



$MYSQL = new Toropyga\DB\MySQL();
$POSTGRESQL = new Toropyga\DB\PostgreSQL();
$ORACLE = new Toropyga\DB\Oracle();
$PDO = new Toropyga\DB\PDOLIB();

/**
 * DBMySQL constructor.
 * Class for working with MySQL database
 * @param mixed $HOST - host
 * @param mixed $PORT - port
 * @param mixed $NAME - DB name
 * @param mixed $USER - user name
 * @param mixed $PASS - user password
 */
$MYSQL = new Toropyga\DB\MySQL($HOST, $PORT, $NAME, $USER, $PASS);

/**
 * PostgreSQL constructor.
 * @param string $HOST - host
 * @param int|string $PORT - port
 * @param string $NAME - database name
 * @param string $USER - user name
 * @param string $PASS - password
 */
$POSTGRESQL = new Toropyga\DB\PostgreSQL($HOST, $PORT, $NAME, $USER, $PASS);

/**
 * DBOracle constructor.
 * @param string $HOST - host
 * @param string $NAME - DB name
 * @param string $USER - user name
 * @param string $PASS - user password
 * @param int $USE_HOST - Oracle connection record type
 * @param string $PORT - port
 * @param bool $P_CONNECT - keep connection for the session
 * @param string $CHARSET - charset
 * @param bool $no_connect - do not connect during initialization
 */
$ORACLE = new Toropyga\DB\Oracle($HOST, $NAME, $USER, $PASS, $USE_HOST, $PORT, $P_CONNECT, $CHARSET, $no_connect);

/**
 * PDOLIB constructor.
 * @param string $db_type - DB type
 * @param string $NAME - DB name
 * @param string $USER - user name
 * @param string $PASS - user password
 * @param string $HOST - host
 * @param string $PORT - port
 * @param int $oracle_connect_type - Oracle connection record type
 */
$PDO = new Toropyga\DB\PDOLIB($db_type, $NAME, $USER, $PASS, $HOST, $PORT, $oracle_connect_type);

$db = Toropyga\DB\DBAPI::connect('postgresql', [
  'host' => '127.0.0.1',
  'port' => 5432,
  'database' => 'app',
  'user' => 'app_user',
  'password' => 'secret',
]);

$users = $db->getQuery('SELECT id, name FROM users', [], 'all');

$api = new Toropyga\DB\DBAPI('pdo_sqlsrv', [
  'host' => 'db.example',
  'database' => 'app',
  'user' => 'app_user',
  'password' => 'secret',
]);
$tables = $api->getTableList();

$tables1 = $MYSQL->getTableList();
$tables2 = $POSTGRESQL->getTableList();
$tables3 = $ORACLE->getTableList();
$tables4 = $PDO->getTableList();

$array = array('field1'=>'value1', 'field2'=>'value2', 'field3'=>'value3');
$index = array('field_where1'=>'value_where1', 'field_where2'=>'value_where2');
$sql_insert1 = $MYSQL->getInsertSQL('table_name', $array);
$sql_update1 = $MYSQL->getUpdateSQL('table_name', $array, $index);
$sql_delete1 = $MYSQL->getDeleteSQL('table_name', $index);

$sql_insert2 = $POSTGRESQL->getInsertSQL('table_name', $array);
$sql_update2 = $POSTGRESQL->getUpdateSQL('table_name', $array, $index);
$sql_delete2 = $POSTGRESQL->getDeleteSQL('table_name', $index);

$sql_insert3 = $ORACLE->getInsertSQL('table_name', $array);
$sql_update3 = $ORACLE->getUpdateSQL('table_name', $array, $index);
$sql_delete3 = $ORACLE->getDeleteSQL('table_name', $index);

$sql_insert4 = $PDO->getInsertSQL('table_name', $array);
$sql_update4 = $PDO->getUpdateSQL('table_name', $array, $index);
$sql_delete4 = $PDO->getDeleteSQL('table_name', $index);

$result1 = $MYSQL->getResults($sql, $one);
$result2 = $POSTGRESQL->getResults($sql, $one);
$result3 = $ORACLE->getResults($sql, $one);
$result4 = $PDO->getResults($sql, $one);

$users = $PDO->getQuery(
    'SELECT id, name FROM users WHERE status = :status',
    ['status' => 'active'],
    'all'
);

$MYSQL->query($sql);