jameslevi/hadron

Is a simple PHP library for MySQL using PDO.

v1.0.0 2021-05-13 16:37 UTC

This package is not auto-updated.

Last update: 2024-04-27 05:30:21 UTC


README

Is a simple PHP library for MySQL using PDO.

Installation

  1. You can install via composer.
composer require jameslevi/hadron
  1. Paste the following code above your project if not using any PHP framework.
require_once __DIR__.'/vendor/autoload.php';
  1. Import hadron into your project.
use Graphite\Component\Hadron\Hadron;

The Basics

This is a basic example on how to implement hadron in your project.

<?php

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

Connecting with your MySQL database

  1. Create a new hadron instance.
$conn = new Hadron('database');
  1. You can also use this magic method.
$conn = Hadron::database();
  1. Set your MySQL credentials.
$conn->setCredentials('username', 'password');
  1. You can also set your server name.
$conn->setServerName('localhost');
  1. You can set your MySQL port number if not using 3306.
$conn->setPort(3303);
  1. Set the default charset from utf8mb4 to your choice.
$conn->setCharset('utf8mb4');
  1. The connection will only be established after calling the connect method.
$conn->connect();
  1. You can determine if connection was established using isConnected method.
$conn->isConnected();
  1. Always close each connection after use.
$conn->close();

Getting data from the database

  1. Use get method when your query expects result from the database.
$query = $conn->query('SELECT first_name, last_name, gender FROM members')->get();
  1. You can count the number of rows returned.
$count = $query->numRows();
  1. You can check if the query returns nothing.
$query->empty();
  1. You can get the first and the last row of the result.
var_dump($query->first());
var_dump($query->last());
  1. You can also get row by index number.
var_dump($query->get(11)); // Return the 11th result.
  1. If query returns only a single row, you can directly get each column.
echo $query->first_name . ' ' . $query->last_name;
  1. And if query returns a multiple row, you can access each column like object properties.
foreach($query->all() as $member)
{
    echo $member->name;
}
  1. You can return the result as an array.
var_dump($query->toArray());
  1. You can also return the result as json.
echo $query->toJson();

Executing Queries

  1. You can use exec method to execute queries expecting no results such as UPDATE, INSERT and DELETE.
$query = $conn->query('UPDATE members SET first_name = :first_name WHERE id = :id');

$query->addParam('first_name', 'James Levi')
      ->addParam('id', 1);
      
$query->exec();
  1. You can determine if query is a success.
$query->success();
  1. You can also know how many rows your query has affected.
echo $query->affectedRows();

Placeholder

Instead of concatenating string to build your SQL query, you can use placeholder to inject values into your SQL query.

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

You can also directly pass your placeholder in the query method.

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

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.