moa19 / session13
Installs: 3
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:TeX
README
Overview
Moa19\Session13
is a PHP package designed for seamless interaction with MySQL databases. It provides an object-oriented approach to building dynamic SQL queries with support for basic database operations such as insert
, select
, update
, delete
, and various types of join
operations. The package is designed to simplify and streamline database interactions with minimal configuration.
This package is ideal for developers looking for an easy-to-use database abstraction layer with method chaining support.
Installation
The package is available on packagist and can be installed easily via Composer.
To install the package, run the following command:
composer require moa19/session13
require_once 'vendor/autoload.php';
After installation, you can include the package in your PHP project:
Usage
Connecting to the Database
To use the package, first create a new instance of the db class, passing your MySQL connection details (host, user, password, database, and table).
use Moa19\Session13\db;
$db = new db('localhost', 'username', 'password', 'database_name', 'table_name');
Inserting Data
To insert data into your table, use the insert method with an associative array where the keys are the column names and the values are the corresponding data.
$data = [ 'name' => 'mohammed abd elhakim', 'email' => 'moa@meaw.com' ]; $db->insert($data)->execute();
3. Selecting Data
To select data, use the select method. You can specify columns, or select all columns using *.
Select specific columns
$db->select('name, email')->where('id', '=', 1)->execute();
Select all columns
$db->select()->where('id', '=', 1)->execute();
To retrieve the results:
All results:
$results = $db->select()->all();
Single result:
$result = $db->select()->get();
4. Updating Data
To update records, use the update method with an associative array of column-value pairs.
$data = [ 'name' => 'Jane Doe' ];
$db->update($data)->where('id', '=', 1)->execute();
Deleting Data
To delete records, use the delete method along with optional conditions.
$db->delete()->where('id', '=', 1)->execute();
6. Performing Joins
You can perform various types of SQL joins: innerJoin, leftJoin, and rightJoin.
Inner Join
$db->innerJoin('users', 'users.id', 'orders.user_id')->select()->execute();
Left Join
$db->leftJoin('users', 'users.id', 'orders.user_id')->select()->execute();
Right Join
$db->rightJoin('users', 'users.id', 'orders.user_id')->select()->execute();
7. Adding Conditions
You can add conditions to your queries using where, Andwhere, and Orwhere.
$db->select()->where('status', '=', 'active')->Andwhere('id', '>', 10)->execute();
8. Executing the Query
To execute the built query and perform the operation (insert, update, delete, etc.), use the execute method.
$affectedRows = $db->execute();
9. Retrieving Results
To retrieve data after running a query:
All results (as an associative array):
$allRows = $db->select()->all(); Single result (first row): $singleRow = $db->select()->get();
Methods
insert($data)
Inserts data into the table.
Parameters
: $data (associative array of column-value pairs).
Returns
: The current instance of the class for method chaining.
select($columns = "*")
Selects columns from the table.
Parameters
: $columns (comma-separated list of column names).
Returns
: The current instance of the class for method chaining.
update($data)
Updates data in the table.
Parameters
: $data (associative array of column-value pairs).
Returns
: The current instance of the class for method chaining.
delete()
Deletes data from the table.
Returns
: The current instance of the class for method chaining.
execute()
Executes the SQL query and returns the number of affected rows.
Returns
: The number of affected rows.
all()
Retrieves all rows from the query result as an associative array.
Returns
: Array of rows.
get()
Retrieves the first row from the query result.
Returns
: Associative array representing the first row.
where($column, $operator, $value)
Adds a WHERE clause to the query.
Parameters
: $column (column name), $operator (comparison operator), $value (value to compare).
Returns
: The current instance of the class for method chaining.
Andwhere($column, $operator, $value)
Adds an AND condition to the query.
Parameters
: Same as where().
Returns
: The current instance of the class for method chaining.
Orwhere($column, $operator, $value)
Adds an OR condition to the query. Parameters: Same as where(). Returns: The current instance of the class for method chaining