Skip to content
Bob Magic II edited this page Feb 19, 2022 · 7 revisions

Installation

composer install netherphp/database

Configuration (JSON)

To prepare configuration automatically from a JSON file add a LoadFromJSON call to your application's main configuration.

Nether\Database\ConnectionConfig::LoadFromJSON(sprintf(
	'%s/conf/env/%s/netherdb.json',
	ProjectRoot,
	ENV
));

Multiple servers can be configured by adding more named entries. The Type field is the name of the PDO driver to use.

{
	"Default": {
		"Type": "mysql",
		"Hostname": "hostname",
		"Database": "database",
		"Username": "username",
		"Password": "password"
	}
}

Configuration (Manual)

Nether\Option::Set([
	'Nether.Database.Connections' => [
		'Default' => new Nether\Database\ConnectionConfig(
			Type: 'mysql',
			Hostname: 'hostname',
			Database: 'database',
			Username: 'username',
			Password: 'password'
		)
	]
]);

Basic Usage

This is a super basic example to get in and get a query done without using any of the fancy features. For advanced things see the various other sections of this wiki.

// collect some data and create a tokenised array keyed in the style
// of pdo bound params, so we can let that handle our injection protection.

$Input = [
	':ID'   => (int)$_POST['ID'],
	':Name' => $_POST['Name']
];

// now hit the database with the info.

$Database = new Nether\Database;
$Result = $Database->Query(
	'UPDATE Users SET Name=:Name WHERE ID=:ID;',
	$Input
);

// decide if we should move on.

if(!$Result->IsOK())
echo $Result->GetError(), PHP_EOL;

else
echo "User {$Input[':ID']} updated.", PHP_EOL;
Clone this wiki locally