https://github.com/yhyasyrian/sydb
Library communication and dealing with databases
https://github.com/yhyasyrian/sydb
database mysqli php php-mysql php-sql sql sydb
Last synced: 3 months ago
JSON representation
Library communication and dealing with databases
- Host: GitHub
- URL: https://github.com/yhyasyrian/sydb
- Owner: yhyasyrian
- Created: 2022-07-18T16:56:22.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-18T16:00:30.000Z (over 1 year ago)
- Last Synced: 2025-02-27T03:20:55.993Z (4 months ago)
- Topics: database, mysqli, php, php-mysql, php-sql, sql, sydb
- Language: PHP
- Homepage:
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SyDb
Syrian DataBase
It is a simple library that depends on connecting to databases via mysql that performs data processing, as it converts ordinary data into `sql` code.
The library is safe and does not contain `sql injection` vulnerabilities. It is intended to avoid these problems, which may be somewhat difficult for beginners to avoid, and it is easy and flexible to learn.## Note
This library was just an experiment and does not have the ability for large and medium projects because of the lack of the ability to merge tables through methods embedded in it, it is useful to add and delete data only and in the end it was just an experience for me and I learned a lot from it and today I do not advise anyone to deal with itIndex A sequential list of methods:
[Install Library](#install-library) How to install library SyDb
[Select Data](#select) Get data from the database
[Insert Data](#insert) Set data from the database
[Update Data](#update) Update data from the database
[Delete Data](#delete) Delete data from the database
[Query SQL](#query) Run SQL code
[Connect DataBase](#connect) New connect database
[Close Connect DataBase](#close) Close the connect present
[Table](#table) Table properties
[Error](#error) Error Exception
Some future:
* [Add data where](#addwhere)
* [Starting column](#startcolumn)
* [Ending column](#endcolumn)
* [Allocate contact time](#settimeconnect)
# install-Library
You can install library with `composer` You can install a library using Composer which is the best way to download SyDb by adding the following line to `composer.json` file:
```json
{
"require": {
"yhyasyrian/sydb": "^1.5"
}
}
```
Or run:
```bash
composer require yhyasyrian/sydb
```# Select
In the "Select" class, there are several methods of use, namely:
`select_sql`, `select`, `fetch_all`, `fetch`Used to fetch data
## select_sql
Use `select_sql` to display an integrated sql code that accepts three parameters:
\$table means the table, type `String`
\$where means where to search, type `Array` value defult="[]", Syntax:
Column name => the value in it
\$etcWhere It is intended to add properties to the search, Methods will be developed for it in future updates, type `String` value defult="".
Return Code Sql, `String` type.Example:
```php
select_sql('memers',['type'=>'admin']);
echo $sql; // SELECT * FROM `memers` WHERE `type`='admin';
// Example $_SESSION['user'] = 'Yhya'
$sql = $SyDb->select_sql('memers',['type'=>'admin','user'=>$_SESSION['user']]);
echo $sql; // SELECT * FROM `memers` WHERE `type`='admin' AND `user`='Yhya';
// Exaple $message = 'Heelo \' Yhya" I am Hak';
$sql = $SyDb->select_sql('message',['message'=>"Heelo ' Yhya\" I am Hak"]);
echo $sql; // SELECT * FROM `message` WHERE `message`='Heelo ' Yhya" I am Hak';
?>
```
## select
Use `select` to run sql code that accepts three parameters:
\$table means the table, type `String`
\$where means where to search, type `Array` value defult="[]", Syntax:
Column name => the value in it
\$etcWhere It is intended to add properties to the search, Methods will be developed for it in future updates, type `String` value defult="".
Return \mysqli_result, `Object` type.Example:
```php
select('memers',['type'=>'band']);
$select->num_rows; // Count rows ; int|string @link https://www.php.net/manual/en/mysqli-result.num-rows.php
// And You Can View All Data
while ($row = $SyDb->fetch($select) /*
* It is shortcut for function mysqli_fetch_assoc
*/) {
print_r($row); // For print this data
}
?>
```
## fetch_all
Use `fetch_all` to select all data that accepts three parameters:
\$table means the table, type `String`
\$where means where to search, type `Array` value defult="[]", Syntax:
Column name => the value in it
\$etcWhere It is intended to add properties to the search, Methods will be developed for it in future updates, type `String` value defult="".
Return Data rows, `Array` type.Example:
```php
fetch_all('memers',['type'=>'active']);
print_r($fetch_all); /* * Output is :
Array(
Array(
[id] => 1,
[type] => "active",
[name] => "Yhya"
),
Array(
[id] => 2,
[type] => "active",
[name] => "Saied"
),
)
*/
?>
```
## fetch
Use `fetch` to select first data that accepts three parameters:
\$table means the table, type `String`
\$where means where to search, type `Array` value defult="[]", Syntax:
Column name => the value in it
\$etcWhere It is intended to add properties to the search, Methods will be developed for it in future updates, type `String` value defult="".
Return Data row, `Array` type.Example:
```php
fetch('memers',['type'=>'active']);
print_r($fetch); /* * Output is :
Array(
[id] => 1,
[type] => "active",
[name] => "Yhya"
)
*/
?>
```
# Insert
In the "Insert" class, there are several methods of use, namely:
`insert_sql`, `insert`Used to insert data
## insert_sql
Use `insert_sql` to display an integrated sql code for add data to database that accepts two parameters:
\$table means the table, type `String`
\$data means data that you want to add into database, type `Array` Syntax:
Column name => the value in it
Return Code Sql, `String` type.Example:
```php
insert_sql('memers',['type'=>'admin','name'=>'Yhya']);
echo $sql; // INSERT INTO `memers` (`type`,`name`) VALUES ('admin','Yhya');
$sql = $SyDb->insert_sql('memers',['type'=>'active','name'=>'Hello I am\' Hack']);
echo $sql; // INSERT INTO `memers` (`type`,`name`) VALUES ('active','Hello I am' Hack');
?>
```
## insert
Use `insert` to run an integrated sql code for add data to database that accepts two parameters:
\$table means the table, type `String`
\$data means data that you want to add into database, type `Array` Syntax:
Column name => the value in it
Return true, `Bool` type.Example:
```php
insert('memers',['type'=>'admin','name'=>'Yhya']); // Adding to the database is done
?>
```
# Update
In the "Update" class, there are several methods of use, namely:
`update_sql`, `update`Used to update data
## update_sql
Use `update_sql` to display an integrated sql code for update data to database that accepts three parameters:
\$table means the table, type `String`
\$where means where data that you want to update into database, type `Array` Syntax:
Column name => the value in it
\$new means data that you want to update into database, type `Array` Syntax:
Column name => the value in it
Return Code Sql, `String` type.Example:
```php
update_sql('memers',['type'=>'admin','name'=>'Yhya'],['status'=>"online"]);
echo $sql; // UPDATE `memers` SET `status`='online' WHERE `type`='admin' AND `name`='Yhya';
$sql = $SyDb->update_sql('memers',['type'=>'active','name'=>'Hello I am\' Hack'],['status'=>"ofline"]);
echo $sql; // UPDATE `memers` SET `status`='ofline' WHERE `type`='active' AND `name`='Hello I am' Hack';
?>
```
## update
Use `update` to run an integrated sql code for update data to database that accepts three parameters:
\$table means the table, type `String`
\$where means where data that you want to update into database, type `Array` Syntax:
Column name => the value in it
\$new means data that you want to update into database, type `Array` Syntax:
Column name => the value in it
Return `Void` type.Example:
```php
update('memers',['type'=>'admin','name'=>'Yhya'],['status'=>"online"]);
$SyDb->update('memers',['type'=>'active','name'=>'Hello I am\' Hack'],['status'=>"ofline"]);
?>
```
# Delete
In the "Delete" class, there are several methods of use, namely:
`delete_sql`, `delete`Used to delte data
## delete_sql
Use `delete_sql` to display sql code that accepts three parameters:
\$table means the table, type `String`
\$where means where to delete from Database, type `Array` Syntax:
Column name => the value in it
Return Code Sql, `String` type.Example:
```php
delete_sql('memers',['type'=>'admin']);
echo $sql; // SELECT * FROM `memers` WHERE `type`='admin';
$sql = $SyDb->delete_sql('message',['message'=>"Heelo ' Yhya\" I am Hak"]);
echo $sql; // DELETE FROM `message` WHERE `message`='Heelo ' Yhya" I am Hak';
?>
```
## delete
Use `delete` to run sql code that accepts three parameters:
\$table means the table, type `String`
\$where means where to delete from Database, type `Array` Syntax:
Column name => the value in it
Return `Void` type.Example:
```php
delete('memers',['type'=>'admin']);
$SyDb->delete('message',['message'=>"Heelo ' Yhya\" I am Hak"]);
?>
```
# Query
In the "Query" class, there are several methods of use, namely:
`query`Used to run sql code
## query
Use `query` to run sql code that accepts one parameters:
\$query means sql code a type mixed
Return `Mixed` typeExample:
```php
query("INSERT INTO `memers` (`type`,`name`) VALUES ('active','Hello I am' Hack');");
// Or
$SyDb->query(
$SyDb->insert_sql('memers',['type'=>'active','name'=>'Hello I am\' Hack'])
);
// Or
$SyDb->insert('memers',['type'=>'active','name'=>'Hello I am\' Hack'])
```{Note}: Using the method can directly cause a sql injection-type hack.
To avoid the problem use the available methods, Example: select, update, etc ...
# Connect
In the "Connect" class, there are several methods of use, namely:
`connect`Used to connect database
## connect
Use connect a database and don't need parameters
Return true, `Bool` typeExample:
```php
connect();
```
{Note}: In PHP CLI between each connection there is an interval (Defult 15s)
# Close
In the "Close" class, there are several methods of use, namely:
`close`Used to end connect database
## close
Use end connect a database and don't need parameters
Return true, `Bool` typeExample:
```php
close();
$Sydb->query('SELECT * FROM `table`'); // Error beacuse not found connect, you can connect with function connect()
```
# Table
In the "Table" class, there are several methods of use, namely:
`viewTable`, `exportTable`, `exportTables`Used to tables a database
## viewTable
Use view a tables in database and don't need parameters
Return all tables, `Array` typeExample:
```php
viewTable();
print_r($tables); /* * Output is:
Array(
[0] => Members,
[1] => Tokens,
[2] => Logs,
[3] => Sitting,
[4] => CronsJob
)
*/
?>
```
## exportTable
Use export a table in database and three parameters:
\$table means the table, type `String`
\$dir means the path for save files in it, type `String`
\$bool for add number time UNIX for name file export, type `Bool`
Return true, `bool` type
```php
exportTable('Members','.'); // you can show file Members.sql
```
## exportTables
Use export a tables in database and tow parameters:
\$dir means the path for save files in it, type `String`
\$bool for add number time UNIX for name file export, type `Bool`
Return true, `bool` type
```php
exportTables('.'); // you can show file Members.sql
```
# Error
In the "Error" class, In order to classify errorsUsed to show errors
## Exception
Example:
```php
getMessage();
} catch (\Throwable $error) {
// The error from your file
echo $error->getMessage();
}
```
# Function Helps
Some functions help this library Example:
* [Add data where](#addwhere)
* [Starting column](#startcolumn)
* [Ending column](#endcolumn)
* [Allocate contact time](#settimeconnect)
## AddWhere
The function renders an array as a generic factor of all connectionsExample:
```php
addWhere(['site'=>($_SERVER['SERVER_NAME'] ?? "CLI")]);
echo $SyDb->select_sql('member',['type'=>'admin']); // SELECT * FROM `member` WHERE `type`='admin' AND `site`='CLI';
echo $SyDb->insert_sql('member',['type'=>'admin','name'=>"Yhya"]); // INSERT INTO `member` (`type`,`name`,`site`) VALUES ('admin','Yhya','CLI');
echo $SyDb->delete_sql('member',['type'=>'banded']); // DELETE FROM `member` WHERE `type`='banded' AND `site`='CLI';
echo $SyDb->update_sql('member',['status'=>'ofline','name'=>"test"],['status'=>"online"]); // UPDATE `member` SET `status`='online' WHERE `status`='ofline' AND `name`='test' AND `site`='CLI';
```
{Advice}: It may be difficult to add the array manually if you want to change your storage format
## StartColumn
If you want get data but start id or last time, you should function `StartColumn` where you can set limit and offset example:
```php
select_sql('member',['type'=>'admin'],$SyDb->StartColumn('id',100,0)); // SELECT * FROM `member` WHERE `status`='admin' ORDER BY id LIMIT 0,100;
```
## EtartColumn
If you want get data but end id or new time, you should function `EtartColumn` where you can set limit and offset example:
```php
select_sql('member',['type'=>'admin']); // SELECT * FROM `member` WHERE `status`='admin' ORDER BY id DESC LIMIT 0,100;
```
## SetTimeConnect
If you use this library with CLI, you can set time connect when every connect such as : you connect every second you don't be forced to create connections new becuse the servers are limting connections example:
```php
setTimeConnect(10);
$startTime = time();
$i = 0;
while ($startTime > (time() - 30)) {
$SyDb->connect();
echo $i++."-number\r";
sleep(1);
}
// Connect 3 item
```
End documentation, if you want any project, you can contact us with email: [email protected]