jameslevi / hadron
Is a simple PHP library for MySQL using PDO.
v1.0.0
2021-05-13 16:37 UTC
Requires
- jameslevi/objectify: ^1.0
This package is not auto-updated.
Last update: 2025-04-26 10:20:55 UTC
README
Is a simple PHP library for MySQL using PDO.
Installation
- You can install via composer.
composer require jameslevi/hadron
- Paste the following code above your project if not using any PHP framework.
require_once __DIR__.'/vendor/autoload.php';
- 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
- Create a new hadron instance.
$conn = new Hadron('database');
- You can also use this magic method.
$conn = Hadron::database();
- Set your MySQL credentials.
$conn->setCredentials('username', 'password');
- You can also set your server name.
$conn->setServerName('localhost');
- You can set your MySQL port number if not using 3306.
$conn->setPort(3303);
- Set the default charset from utf8mb4 to your choice.
$conn->setCharset('utf8mb4');
- The connection will only be established after calling the connect method.
$conn->connect();
- You can determine if connection was established using isConnected method.
$conn->isConnected();
- Always close each connection after use.
$conn->close();
Getting data from the database
- Use get method when your query expects result from the database.
$query = $conn->query('SELECT first_name, last_name, gender FROM members')->get();
- You can count the number of rows returned.
$count = $query->numRows();
- You can check if the query returns nothing.
$query->empty();
- You can get the first and the last row of the result.
var_dump($query->first()); var_dump($query->last());
- You can also get row by index number.
var_dump($query->get(11)); // Return the 11th result.
- If query returns only a single row, you can directly get each column.
echo $query->first_name . ' ' . $query->last_name;
- And if query returns a multiple row, you can access each column like object properties.
foreach($query->all() as $member) { echo $member->name; }
- You can return the result as an array.
var_dump($query->toArray());
- You can also return the result as json.
echo $query->toJson();
Executing Queries
- 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();
- You can determine if query is a success.
$query->success();
- 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.