jameslevi / neutrino
Is a simple database library using PDO in PHP.
Requires
- jameslevi/objectify: ^1.0
This package is not auto-updated.
Last update: 2025-05-01 18:38:47 UTC
README
Is a simple database library using PDO in PHP.
Supported Drivers
- MySQL
- Microsoft SQL Server
Installation
You can install via composer.
composer require jameslevi/neutrino
Add the composer autoload to your project.
require_once __DIR__ . '/vendor/autoload.php';
Import neutrino into your project.
use Graphite\Component\Neutrino\Neutrino;
The Basics
<?php // add the composer autoload. require_once __DIR__ . '/vendor/autoload.php'; // import neutrino into your project. use Graphite\Component\Neutrino\Neutrino; // create a new neutrino instance. $db = new Neutrino('mysql'); // set the database to access. $db->setDatabase('your_database'); // set credentials for authentication. $db->setUsername('user1'); $db->setPassword('your_password'); // set server port. $db->setPort(3306); // establish connection. $db->connect(); // check if connection was established. if(!$db->isConnected()) { die('Connection Failed'); } // set SQL query to execute. $query = $db->query('SELECT * FROM users WHERE id = :id'); // set placeholder value. $query->addIntParam('id', 1); // get the query result. $fetch = $query->get(); // Convert result to json. echo $fetch->toJson(); // close the connection. $db->close();
Establishing Connection
You can establish connection by providing the name of your database, server name, username and password.
// create a new neutrino instance. $db = new Neutrino('sqlsrv'); // set the database to use. $db->setDatabase('mydatabase'); // set authentication username. $db->setUsername('your_username'); // set authentication password. $db->setPassword('your_password'); // set port number if required. $db->setPort(1433); // establish connection. $db->connect();
Always remember to close the connection each time after use.
$db->close();
Establishing Connection using DSN String
// create a new neutrino instance. $db = new Neutrino('sqlsrv'); // set dsn string. $db->setDsn('server=localhost;database=your_database'); // Set username if required. $db->setUsername('your_username'); // Set password if required. $db->setPassword('your_password'); // establish connection. $db->connect();
Basic Query
Use get method if expecting results such as SELECT queries.
$query = $db->query('SELECT * FROM members')->get();
Use exec method if no result is expected such as UPDATE, INSERT or DELETE queries.
$db->query('DELETE FROM members WHERE id = 1')->exec();
Parameters
You can bind values indirectly to your SQL script.
// your SQL script. $query = $db->query('SELECT * FROM members WHERE id = :id LIMIT :start, :offset'); // bind values into your SQL script. $query->addParam('id', 1) ->addParam('start', 0) ->addParam('offset', 10); // execute the query. $fetch = $query->get();
Parameter Data Types
You can declare the data type of each parameters.
$query->addParam('id', 1, PDO::PARAM_INT);
Parameter data types supports string, integer, boolean and null.
$db->addStringParam('id', '1'); // Value has string data type. $db->addIntegerParam('id', 1); // Value has integer data type. $db->addBooleanParam('id', true); // Value has boolean data type. $db->addNullParam('id', null); // Value has null data type.
Fetching Results
You can return the list of result using fetch method.
$rows = $db->query('SELECT email FROM members')->get()->fetch(); // list all emails. foreach($rows as $row) { echo $row->email . '<br>'; }
Get Result Helper Methods
You can get row by index number in a result list.
$get = $db->query('SELECT email FROM members')->get(); // Get the first row from result. echo $get->get(0)->email;
You can easily get the first and last row from the result list.
$get = $db->query('SELECT email FROM members')->get(); // Get the first email from the results. echo $get->first()->email; // Get the last email from the results. echo $get->last()->email;
You can return a list of values from a single column.
var_dump($get->pluck('email')); // Get all emails from the query.
You can return the column names available from the result.
var_dump($get->columnNames());
You can determine the number of rows from the result.
echo $get->numRows();
Return result as array.
var_dump($get->toArray());
Return result as json.
echo $get->toJson();
PDO Attributes
You can set PDO attributes before establishing the connection.
$db->addOption(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Error Handling
Speficy how the driver will report errors. Values can be silent, warning or exception.
$db->setErrorMode('exception'); // or $db->errorModeException();
Column Name Cases
Specify the case of the column names.
$db->lowercase(); // Causes column names to lowercase. $db->natural(); // Display column names as returned by the database. $db->uppercase(); // Causes column names to uppercase.
Transformations
Convert numeric values into string.
$db->stringify();
Convert empty string to null.
$db->setEmptyStringToNull();
Convert null to empty string.
$db->setNullToEmptyString();
Buffer Size
Value might vary depending on driver.
$db->setMaxBufferSize(1024);
Buffered Queries
Force queries to be buffered. Only available in MySQL.
$db->useBufferedQuery();
Timeout
Set the query timeout in seconds. Only available in Microsoft SQL Server.
$db->setTimeout(20);
Contribution
For issues, concerns and suggestions, you can email James Crisostomo via nerdlabenterprise@gmail.com.
License
This package is an open-sourced software licensed under MIT License.