PHP code example of jameslevi / neutrino

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

    

jameslevi / neutrino example snippets




use Graphite\Component\Neutrino\Neutrino;



// add the composer autoload.
t.
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();

// 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 

$db->close();

// create a new neutrino instance.
$db = new Neutrino('sqlsrv');

// set dsn string.
$db->setDsn('server=localhost;database=your_database');

// Set username if 

$query = $db->query('SELECT * FROM members')->get();

$db->query('DELETE FROM members WHERE id = 1')->exec();

// 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();

$query->addParam('id', 1, PDO::PARAM_INT);

$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.

$rows = $db->query('SELECT email FROM members')->get()->fetch();

// list all emails.
foreach($rows as $row)
{
    echo $row->email . '<br>';
}

$get = $db->query('SELECT email FROM members')->get();

// Get the first row from result.
echo $get->get(0)->email;

$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;

var_dump($get->pluck('email')); // Get all emails from the query.

var_dump($get->columnNames());

echo $get->numRows();

var_dump($get->toArray());

echo $get->toJson();

$db->addOption(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->setErrorMode('exception');

// or

$db->errorModeException();

$db->lowercase(); // Causes column names to lowercase.
$db->natural(); // Display column names as returned by the database.
$db->uppercase(); // Causes column names to uppercase.

$db->stringify();

$db->setEmptyStringToNull();

$db->setNullToEmptyString();

$db->setMaxBufferSize(1024);

$db->useBufferedQuery();

$db->setTimeout(20);