EasySQL remarkably reduces the amount of code you need for performing CRUD database operations by taking over bulk redudant tasks, and letting you concentrate on the important stuff.
Code less, do extra!
* See demo for a sample case study
- Current version: v1.0.0 - 145 kb gzipped
If you have been using PHP to perform CRUD (Create, Read, Update, Delete) operations to your database, you've probably been writing a lot of code. Whether you're a newbie to or a novice at PHP, you are most likely familiar with how much code is needed in order to connect to a database, perform a simple operation such as reading data from a table, or log errors for debugging.
With EasySQL, you can make your coding much easier by writing less code: you don't need to worry if you've used prepared statements each time you make a CRUD operation, if you've escaped the data to prevent a security breach, or if you've used the correct SQL syntax. All this is handled for you so that you can concentrate on your project with your mind at ease, assured that issues such as SQL injection are already taken care of.
To get a better grasp of this, have a look at the demo.
EasySQL uses PHP 7.0, which is a powerful server scripting language.
EasySQL uses the standard language for accessing databases.
EasySQL employs PDO, which works on 12 different database systems.
EasySQL is negligible in size (only 145 kb gzipped).
JSON can be read and used as a data format by any programming language.
Enjoy extra functionality such as logs minification, and backtrace objects.
EasySQL is simple to use; just include it in your script and you're good to go!
EasySQL can work on any browser and server capable of processing PHP.
To illustrate the power of EasySQL, we will create the backend of a simple Contact Manager application. We will use EasySQL to retrieve contacts from a sample database, add a new contact to the collection, and perform several other operations.
Alongside each operation, we will also show the amount of code you'd have required if you had not been using EasySQL, whilst still maintaing core features such as avoiding SQL injection by using prepared statements.
Let's code away!
id | first_name | last_name | phone_number |
---|---|---|---|
1 | Hello | World | 1234567890 |
2 | Lorem | Ipsum | 0987654321 |
mysql --user=username --password=user_password
use sample_db
<?php
$servername = "localhost";
$username = "username";
$password = "user_password";
$database = "sample_db";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
}
?>
<?php
$conn = new easysql();
$conn->set_credentials("mysql", "localhost", "username", "user_password", "sample_db");
?>
{
"code": 200,
"status": "success",
"data": null
}
Lines of code so far: 11 | 2
SELECT `id`, `first_name`, `last_name`, `phone_number` FROM `contacts`
<?php
$result = [];
try {
$stmt = $conn->prepare("SELECT `id`, `first_name`, `last_name`, `phone_number` FROM `contacts`");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
$result[] = $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
$conn->select("contacts", ["id", "first_name", "last_name", "phone_number"]);
?>
{
"code": 200,
"status": "success",
"data": [
{
"id": 1,
"first_name": "Hello",
"last_name": "World",
"phone_number": "1234567890"
},
{
"id": 2,
"first_name": "Lorem",
"last_name": "Ipsum",
"phone_number": "0987654321"
}
]
}
Lines of code so far: 23 | 3
SELECT `first_name`, `last_name`, `phone_number` FROM `contacts` WHERE `id` = '2'
<?php
$result = [];
try {
$stmt = $conn->prepare("SELECT `id`, `first_name`, `last_name`, `phone_number` FROM `contacts` WHERE `id` = '2'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
$result[] = $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
$conn->select("contacts", ["first_name", "last_name", "phone_number"], ["id" => 2]);
?>
{
"code": 200,
"status": "success",
"data": [
{
"first_name": "Lorem",
"last_name": "Ipsum",
"phone_number": "0987654321"
}
]
}
Lines of code so far: 35 | 4
SELECT `id`, `first_name`, `last_name`, `phone_number` FROM `contacts` ORDER BY `id` DESC
<?php
$result = [];
try {
$stmt = $conn->prepare("SELECT `id`, `first_name`, `last_name`, `phone_number` FROM `contacts` ORDER BY 'id' DESC");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
$result[] = $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
$conn->select("contacts", ["id", "first_name", "last_name", "phone_number"], null, ["id", "DESC"]);
?>
{
"code": 200,
"status": "success",
"data": [
{
"id": 2,
"first_name": "Lorem",
"last_name": "Ipsum",
"phone_number": "0987654321"
},
{
"id": 1,
"first_name": "Hello",
"last_name": "World",
"phone_number": "1234567890"
}
]
}
Lines of code so far: 47 | 5
SELECT `phone_number` FROM `contacts` WHERE `first_name` LIKE 'Lorem'
<?php
$result = [];
try {
$stmt = $conn->prepare("SELECT `phone_number` FROM `contacts` WHERE `first_name` LIKE 'Lorem'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
$result[] = $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
$conn->select2("contacts", ["phone_number"], "`first_name` LIKE 'Lorem'");
?>
{
"code": 200,
"status": "success",
"data": [
{
"phone_number": "0987654321"
}
]
}
Lines of code so far: 59 | 6
UPDATE `contacts` SET `phone_number` = '3268790920' WHERE `first_name` = 'Lorem'
<?php
try {
$sql = "UPDATE `contacts` SET `phone_number` = '3268790920' WHERE `first_name` = 'Lorem'";
$stmt = $conn->prepare($sql);
$stmt->execute();
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
?>
<?php
$conn->update("contacts", ["phone_number" => "3268790920"], ["first_name" => "Lorem"]);
?>
{
"code": 200,
"status": "success",
"data": null
}
Lines of code so far: 67 | 7
INSERT INTO `contacts` (`first_name`, `last_name`, `phone_number`) VALUES ('Neil', 'Patrick Harris', '7854203248')
<?php
try {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("INSERT INTO `contacts` (`first_name`, `last_name`, `phone_number`) VALUES (:first_name, :last_name, :phone_number)");
$_insert = [
"first_name" => "Neil",
"last_name" => "last_name",
"phone_number" => "7854203248"
];
foreach ($_insert as $key => $value) {
$stmt->bindParam($key, $value);
}
$stmt->execute();
echo "New id: " . $conn->lastInsertId();
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
?>
<?php
$conn->insert("contacts", ["first_name" => "Neil", "last_name" => "Patrick Harris", "phone_number" => "7854203248"]);
?>
{
"code": 200,
"status": "success",
"data": [
{
"id": 3
}
]
}
Lines of code so far: 84 | 8
DELETE FROM `contacts` WHERE `first_name` = 'Neil'
<?php
try {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM `contacts` WHERE `first_name` = 'Neil'";
$conn->exec($sql);
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
$conn->delete("contacts", ["first_name" => "Neil"]);
?>
{
"code": 200,
"status": "success",
"data": null
}
Lines of code so far: 92 | 9
ALTER TABLE `contacts` ADD `email_address` VARCHAR(255) NOT NULL;
<?php
try {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("ALTER TABLE `contacts` ADD `email_address` VARCHAR(255) NOT NULL");
$stmt->execute();
}
catch(PDOException $e) {
echo "Error:" . $e->getMessage();
}
?>
<?php
$conn->alter("contacts", "ADD", ["email_address" => "VARCHAR(255) NOT NULL"]);
?>
{
"code": 200,
"status": "success",
"data": null
}
Lines of code so far: 100 | 10
That's it! I hope I've made your coding much easier.
Please remember to share this with your friend and a fellow dev if it has benefitted you.
You can also raise an issue or contribute to the project on Github.