A php class for managing and connecting to a database

Overview

Query builder class php

This class is responsible for creating and executing sql commands and helps you to execute as easily as possible and safely.

Installation

You can use a composer to install the package

$ composer require mhmmdq/database

How to connect to the database

To connect to the database, you need to send data to the connection class, which must be done as follows

Driver The type of database driver to connect

Host Database server

Port Database port - 3306 by default

Username Database login username

Password Database login password

Charset String encoding type - utf8mb4 by default

Collation Letter comparison method - a default of utf8mb4_general_ci

Database Database name

<?php

/* Add autoloader to php file */
include './vendor/autoload.php';


use Mhmmdq\Database\Connection;
new Connection([
    'driver'=>'mysql',
    'host'=>'localhost',
    'port'=>'',
    'username'=>'root',
    'password'=>'',
    'charset'=>'utf8mb4',
    'collation'=>'utf8mb4_general_ci',
    'database'=>'oop'
]);

Methods of receiving data

There are different types of methods defined for retrieving data from a database that you can use

Query the builder query class

You must first add a query builder class to your work

use Mhmmdq\Database\QueryBuilder as DB;

$db = new DB();

Select the table name

To select a table name, use the table method and give it the name of the desired table as input

$db->table('users');

Capture all table outputs

You can use the get method to get all the records of a table and run a query

$users = $db->table('users')->get();
var_dump($users);

But if you want to have Json output, you can get help from toJson

$users = $db->table('users')->toJson();
echo $users;

In this case, you also change the type of file sent to json. If you do not want this to happen, enter false toJson function.

$users = $db->table('users')->toJson(false);
echo $users;

You can even receive output as a presentation

$users = $db->table('users')->toArray();
var_dump($users);
Number of outputs per query

The number of all output rows is available as follows

$users = $db->table('users')->get();
echo $users->rowCount;

Select custom column names

You can output from any column you just need to use the select method

$users = $db->table('users')->select('username,email')->toJson();
echo $users;

Sorting outputs

Adjust the display of outputs

$users = $db->table('users')->orderBy('id','DESC')->get();

Applied methods

count()

Count all rows in a table in primarykey

$db->table('users')->count();
max()

Find the largest value of a column in a table

$db->table('users')->max('score');
min()

Find the smallest value of a column in a table

$db->table('users')->min('score');

Restrict outputs by performing operations where

You can use the where method to receive filtered data

The first type of use

Restriction based on primarykey Normally the primarykey is equal to id. You can do this to change

$db->primaryKey('columnName');

Now, if you do not need this function, you can directly use the following method to filter with id value

$users = $db->table('users')->where('6')->get();

Here only the user with an id equal to 6 is displayed. In fact, the following query is executed

SELECT * FROM `users` WHERE `users`.`id` = 6;
The second method uses where

If you are looking for a column other than the primary key, you can do this

$users = $db->table('users')->where('username','mhmmdq')->get();

In this case, from the users table of the username column, only the user with the username mhmmdq is selected and the following query is executed

SELECT * FROM `users` WHERE `users`.`username` = 'mhmmdq';
The second method uses where to change the operator

If you want to use another operator to search for another column, you can do the following

$users = $db->table('users')->where('name','LIKE','%Mohammad%')->get();
Multiple use of where

You can use any amount you want where

$users = $db->table('users')->where('name','mohammad')->where('age','>','18')->get();

Limitation by number

If you want to display a certain number of records

$users = $db->table('users')->limit(6)->get()

Get the first output

$user = $db->table('users')->first()

Find method and findorfail

find()

This method uses a template to find a record in the database and displays the output

$user = $db->table('users')->find('username','mhmmdq');

If you want to output with another data type, you can enter json or array as the last input

$user = $db->table('users')->find('username','mhmmdq','json');
findOrFial()

This function allows you to go to page 404 if there is no record with the specifications, but you need to specify the location of the view file.

$db->notFoundView($path);

After the introduction, if the output is zero, it will be transferred to page 404

$user = $db->table('users')->findOrFail('username','mhmmdq');

Pagination

Output pagination of database records along with page links Follow the steps below to paginate

$users = $db->table('users')->pagination(5)->get()

In this way, 5 users are displayed on each page Note that after enabling this feature, $ _GET ['page'] is used by the class to identify the current page

Get links to pages

In the simplest way possible, just print

echo $db->links();

But if you want to personalize

echo $db->links([
        'linksNumber'=>'8',
        'classList'=>[
            'nav'=>'Page navigation example',
            'ul'=>'pagination',
            'li'=>'page-item',
            'li:active'=>'active',
            'a'=>'page-link'
        ]
]);

This is the way it works

Record data in the database

To register information in the database, you will spend a little time, just enter the data as a presentation to the insert method to enter the information into the database.

$db->insert('users',[
   'username'=>'user1',
   'email'=>'[email protected]',
   'password'=>password_hash('12345678',PASSWORD_DEFAULT),
]);

The information is easily entered into your database, but it is still there. If you need validation, you can leave it to us.

validation

Available validation methods

max:value Maximum allowed characters

min:value Minimum allowed characters

email Check the authenticity of the email

uniq Unique search without data

Make a array and get started

$validate = [
    'username'=>'uniq|min:6|max:255',
    'email'=>'uniq|email'
];

And now insert the variable

$db->insert('users',[
   'username'=>'user1',
   'email'=>'[email protected]',
   'password'=>password_hash('12345678',PASSWORD_DEFAULT),
],$validate);

Now, before registration in the database, validation is done

Editing database data

Editing information is as simple as the rest of the operations

$db->update('users',[
        'username'=>'mhmmdqasemi'
],['username','mhmmdq']);

update($table , $data , $where , $validate)

$validate = [
    'username'=>'uniq|min:6|max:255',
];
$db->update('users',[
        'username'=>'mhmmdqasemi'
],['username','mhmmdq'],$validate);

Delete records from the database

delete($table , $where)

$db->delete('users',['id','8']);
You might also like...
ORM layer that creates models, config and database on the fly

RedBeanPHP 5 RedBeanPHP is an easy to use ORM tool for PHP. Automatically creates tables and columns as you go No configuration, just fire and forget

A Redis based, fully automated and scalable database cache layer for Laravel
A Redis based, fully automated and scalable database cache layer for Laravel

Lada Cache A Redis based, fully automated and scalable database cache layer for Laravel Contributors wanted! Have a look at the open issues and send m

The query filter bundle allows you to filter data from QueryBuilder and the Database
The query filter bundle allows you to filter data from QueryBuilder and the Database

The query filter bundle allows you to filter data from QueryBuilder and the Database. you can filter multiple columns at the same time and also you can filter relation fields with two-level deep and without any join in your query builder.

A link database for discovering cool and interesting websites

Yesterlinks Yesterlinks is a database of links to unique or interesting websites. This is a project, in its infancy, designed to be a directory of lin

 Low code , Zero Configuration ORM that creates models, config, database and tables on the fly.
Low code , Zero Configuration ORM that creates models, config, database and tables on the fly.

๐Ÿš€ ARCA ORM ๐Ÿ”ฅ Low code , Zero Configuration ORM that creates models, config, database and tables on the fly. ๐Ÿ”ฅ ๐Ÿ‡ฎ๐Ÿ‡ณ Made in India ๐Ÿ‡ฎ๐Ÿ‡ณ Complete docu

 The query sorting bundle allows you to sort data from QueryBuilder and the Database
The query sorting bundle allows you to sort data from QueryBuilder and the Database

The query sorting bundle allows you to sort data from QueryBuilder and the Database. you can sort multiple columns at the same time and also you can sort relation fields with two-level deep and without any join in your query builder.

The lightweight PHP database framework to accelerate development
The lightweight PHP database framework to accelerate development

The lightweight PHP database framework to accelerate development Features Lightweight - Less than 100 KB, portable with only one file Easy - Extremely

PHP Database Migrations for Everyone
PHP Database Migrations for Everyone

Phinx: Simple PHP Database Migrations Intro Phinx makes it ridiculously easy to manage the database migrations for your PHP app. In less than 5 minute

Database management in a single PHP file

Adminer - Database management in a single PHP file Adminer Editor - Data manipulation for end-users https://www.adminer.org/ Supports: MySQL, MariaDB

Releases(1.1.2)
Owner
Mohammad Qasemi
Web designer and developer
Mohammad Qasemi
Phpstan-dba - database handling related class reflection extension for PHPStan & framework-specific rules

database handling class reflection extension for PHPStan This extension provides following features: PDO->query knows the array shape of the returned

Markus Staab 175 Dec 29, 2022
A Symfony application for managing and automating regular backups of MySQL databases.

DbSaver DbSaver is an application written by Bastien LOUGHIN allowing you to make automatic daily backups (and manual backups) for your MySQL database

Bastien 35 Nov 11, 2022
SleekwareDB is a NoSQL database storage service. A database storage service that can be used for various platforms and is easy to integrate.

SleekwareDB is a NoSQL database storage service. A database storage service that can be used for various platforms and is easy to integrate. NoSQL API

SleekwareDB 12 Dec 11, 2022
Pure PHP NoSQL database with no dependency. Flat file, JSON based document database.

Please give it a Star if you like the project ?? โค๏ธ SleekDB - A NoSQL Database made using PHP Full documentation: https://sleekdb.github.io/ SleekDB i

Kazi Mehedi Hasan 745 Jan 7, 2023
Connect and work with MySQL/MariaDB database through MySQLi in PHP. This is an introductory project, If you need a simple and straightforward example that takes you straight to the point, you can check out these examples.

First MySQLi PHP Connect and work with MySQL/MariaDB database through MySQLi in PHP. The above exercises are designed for students. This is an introdu

Max Base 4 Feb 22, 2022
Clean up your Magento database by removing orphaned, unused and wrongly added attribute, attribute values and settings (for M2).

Magento 2 EAV Cleaner Console Command Purpose of this project is to check for different flaws that can occur due to EAV and provide cleanup functions.

FireGento e. V. - Hackathons 41 Dec 14, 2022
A simple library to access and manipulate database records. Built on top of Dibi and hardwired for PostgreSQL.

grifart/tables A simple library to access and manipulate database records. Built on top of Dibi and hardwired for PostgreSQL. This library is develope

GRIFART 5 Nov 11, 2022
This package provides a framework-agnostic database backup manager for dumping to and restoring databases from S3, Dropbox, FTP, SFTP, and Rackspace Cloud

Database Backup Manager This package provides a framework-agnostic database backup manager for dumping to and restoring databases from S3, Dropbox, FT

Backup Manager 1.6k Dec 23, 2022
A complete, simple and powerful database framework written in PHP

BaseSQL BaseSQL is a complete database framework written in PHP. It was built to accelerate projects development by handle database connections and qu

Willian Pinheiro 2 Sep 21, 2021
The Enobrev\ORM library is a small framework of classes meant to be used for simply mapping a mysql database to PHP classes, and for creating simply SQL statements using those classes.

The Enobrev\ORM library is a small framework of classes meant to be used for simply mapping a mysql database to PHP classes, and for creating simply SQL statements using those classes.

Mark Armendariz 0 Jan 7, 2022