PHP code example of peterujah / db-controller

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

    

peterujah / db-controller example snippets


use Peterujah\NanoBlock\DBController;
// Pass the configuration as an array
$config = [
    'VERSION' => 'mysql',
    'HOST' => 'localhost',
    'PORT' => 3306,
    'NAME' => 'my_database',
    'USERNAME' => 'root',
    'PASSWORD' => 'password',
];

$handler = new DBController($config);

class Conn extends \Peterujah\NanoBlock\DBController{ 
	public function __construct(bool $development = false){
 		$config = array(
			"PORT" => 3306,
			"HOST" => "localhost",
			"VERSION" => "mysql",
		);
		if($development){
			$config["USERNAME"] = "root";
			$config["PASSWORD"] = "";
			$config["NAME"] = "dbname";
		}else{
			$config["USERNAME"] = "dbusername";
			$config["PASSWORD"] = "dbpass";
			$config["NAME"] = "dbname";
		} 
		$this->onDebug = $development;
		parent::__construct($config);
	}
}
 
$handler = new Conn($_SERVER["HOST_NAME"]=="localhost");

$handler->prepare('SELECT * FROM users WHERE username = :username LIMIT 1');
$handler->bind(':username', "Peter");
$handler->execute();
$res = $handler->getOne();
$handler->free();

$handler->query('SELECT * FROM users');
$res = $handler->getAll();
$handler->free();

// Set a configuration value
$handler->setConfig('VERSION', 'pgsql');

// Enable debugging mode
$handler->setDebug(true);


// Get the error information for the last statement execution
$errorInfo = $handler->error();

// Print the error message
if ($errorInfo !== null) {
    echo "Error: " . $errorInfo[2];
}

// Enable debugging mode
$handler->setDebug(true);

// Dump the debug information for the last statement execution
$handler->dumpDebug();


// Prepare a statement
$query = 'SELECT * FROM users WHERE id = :id';
$handler->prepare($query);

// Bind values to parameters
$handler->bind(':id', 1);

//Binds a variable to a parameter.
$handler->param(':id', 1, DBController::_INT)

// Execute the statement
$handler->execute();

// Fetch a single row as an object
$user = $handler->getOne();

// Fetch all rows as an array of objects
$users = $handler->getAll();

// Get the number of rows affected by the last statement execution
$rowCount = $handler->rowCount();

// Get the last inserted ID
$lastInsertId = $handler->getLastInsertId();

// Free up the statement cursor
$handler->free();
 
[
     PORT => 3306,
     HOST => "localhost",
     VERSION => "mysql",
     NAME => "dbname",
     USERNAME => "root",
     PASSWORD => ""
]