PHP MySQL Database Class allows you to write less and do more.
- PHP >= 5.5
- PHP extensions (mysqli)
-
Prepare your MySQL database
-
Clone the repo
git clone https://github.com/MimoudiX/PHP-MySQL-Database-Class.git
-
Enter your database informations in
config/config.php
$database_connection->server = 'localhost'; $database_connection->username = 'root'; $database_connection->password = ''; $database_connection->name = 'dbname';
Include bootstrap.php
in your php file
include_once 'bootstrap.php';
Let's pretend we have a table called pages
page_id | title | url | description |
---|
The following PHP inserts a new record in pages
table:
insert($table, $data = [], $clean = true)
//$clean is true by default, turn it false if you want to avoid cleaning strings
Database::insert('pages', ['title' => 'title here', 'url' => 'url here', 'description' => 'description here'], false);
The selection from the pages
table will now look like this:
page_id | title | url | description |
---|---|---|---|
1 | title here | url here | description here |
The following PHP deletes a record in pages
table where page_id
is 1
:
delete($from, $conditions = [])
Database::delete('pages', ['page_id' => '1']);
//you can write more conditions
Database::delete('pages', ['page_id' => '1',"title" => "title here"]);
//all conditions must be true or the record won't be identified
The following PHP will update title
value in pages
table where page_id
is 1
:
update($what, $fields = [], $conditions = [])
Database::update('pages', ['title' => 'new title here'], ['page_id' => '1']);
The selection from the pages
table will now look like this:
page_id | title | url | description |
---|---|---|---|
1 | new title here | url here | description here |
The following PHP will check if page_id
exists in pages
table where title == some title
exists($what = [], $from, $conditions = [])
if(Database::exists('page_id', 'pages', ['title' => 'some title'])) {
echo "page found";
} else {
echo "page not found";
}
The following PHP will get title
value where page_id
is 1
simple_get($raw_what, $from, $conditions = [])
$title = Database::simple_get('title', 'pages', ['page_id' => 1]);
echo $title;
//will print "new title here"
The following PHP is the same but will be capable of getting multiple values
get($what = [], $from, $conditions = [], $order = false, $clean = false)
$data = Database::get(['title', 'description'], 'pages', ['page_id' => 4]);
echo $data->title . "\n" . $data->description;
//will print new title here description here
you can also try *
to get all values
$data = Database::get(['*'], 'pages', ['page_id' => 1]);
echo $data->page_id . $data->title . $data->url . $data->description;
MimoudiX - @MimoudiX - [email protected]