PHP code example of jameslevi / hadron

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




use Graphite\Component\Hadron\Hadron;



// Import hadron into your project.
use Graphite\Component\Hadron\Hadron;

// Create a new Hadron instance.
$conn = new Hadron('database');

// Set your MySQL username and password.
$conn->setCredentials('username', 'password');

// Create a new query.
$query = $conn->query('SELECT * FROM users WHERE id = :id LIMIT :start, :offset');

// Set the value of the parameters.
$query->addParam('id', 1)
      ->addParam('start', 0)
      ->addParam('offset', 10);

// Get the results from the query.
$results = $query->get();

// Close connection from the database.
$conn->close();

$conn = new Hadron('database');

$conn = Hadron::database();

$conn->setCredentials('username', 'password');

$conn->setServerName('localhost');

$conn->setPort(3303);

$conn->setCharset('utf8mb4');

$conn->connect();

$conn->isConnected();

$conn->close();

$query = $conn->query('SELECT first_name, last_name, gender FROM members')->get();

$count = $query->numRows();

$query->empty();

var_dump($query->first());
var_dump($query->last());

var_dump($query->get(11)); // Return the 11th result.

echo $query->first_name . ' ' . $query->last_name;

foreach($query->all() as $member)
{
    echo $member->name;
}

var_dump($query->toArray());

echo $query->toJson();

$query = $conn->query('UPDATE members SET first_name = :first_name WHERE id = :id');

$query->addParam('first_name', 'James Levi')
      ->addParam('id', 1);
      
$query->exec();

$query->success();

echo $query->affectedRows();

$query = $conn->query('INSERT members (`first_name`,`last_name`,`gender`) VALUES(:first_name, :last_name, :gender)');

$query->addParam('first_name', 'James Levi')
      ->addParam('last_name', 'Crisostomo')
      ->addParam('gender', 'male');
      
$query->exec();

$query = $conn->query('SELECT * FROM members WHERE id = :id', array('id' => 1))->get();