syamsoul/php-db-wow

This package will make your life easier in handling MySQL database.

2.0.0 2022-12-19 04:46 UTC

This package is auto-updated.

Last update: 2024-11-19 09:12:55 UTC


README

Latest Version on Packagist

Documentation, Installation and Usage Instructions

See the documentation for detailed installation and usage instructions.

   

Introduction

This package will make your life easier in handling MySQL database.

 

   

Requirement

  • PHP version: 5.5.0 and above

   

Installation

This package can be used in PHP 5.5 or higher. If you are using an older version of PHP, there's might be some problem. If there's any problem, you can create new issue and I will fix it as soon as possible.

You can install the package via composer:

composer require syamsoul/php-db-wow

   

Usage & Reference

* Before you read this section, you can take a look the example below to make it more clear to understand.

 

How to use it?

First, you must add this line to your PHP file:

use SoulDoit\PhpDBWow\DB;

 

And then create a new DB instance:

$db = new DB($hostname, $db_name, $db_username, $db_password);

Which is:

  • $hostname is a string of your server hostname, for example:
$hostname = 'localhost';

// or

$hostname = 'mysql.hostinger.my';

 

  • $db_name is a string of your database name, for example:
$db_name = 'new_project_db';

 

  • $db_username is a string of your MySQL's username, for example:
$db_username = 'root';

 

  • $db_password is a string of your MySQL's password, for example:
$db_password = 'mypassword';

 

 

And that's it. Now you have a connection with database. Congrats!

Next step is how to run the sql queries. Just head off to the example section and I'm sure you'll understand.

Enjoy! :D

 

 

Example

// Autoload files using the Composer autoloader.
require_once  __DIR__  .  '/vendor/autoload.php';

use SoulDoit\PhpDBWow\DB;

$db = new DB('localhost', 'test_blank', 'root', '');
  

// *******
// INSERT
// *******

$table="shoes";

$parameters = [
    "brand" => "Lee",
    "price" => 543.23
];

$result = $db->insert($table, $parameters);

if($result === false) echo "Failed";
else echo "Success! The inserted ID is ".$result;
  
  

// *******
// DELETE
// *******

$table="shoes";

$conditions = [
    "id" => 2,
];

if($db->delete($table, $conditions) === false) echo "Failed";
else echo "Success! The item is deleted";

  

// *******
// UPDATE
// *******

$table="shoes";

$conditions = [
    "id" => 3,
];

$parameters = [
    "brand" => "Puma",
];

if($db->update($table, $conditions, $parameters) === false) echo "Failed";
else echo "Success! The item is updated";

  

// *******
// SELECT SINGLE DATA
// *******

$table = "shoes";

$conditions = [
    "id" => 3,
];

$data = $db->select($table, $conditions)->first();

if($data === false) echo "Failed";
else {
    echo "Success! \n";
    echo "Return Data: " . $data['brand'];
}


// *******
// SELECT MULTIPLE DATA
// *******

$table = "shoes";

$conditions = [
    "is_for_sale" => 1,
];

$data = $db->select($table, $conditions)->get();

if($data === false) echo "Failed";
else {
    echo "Success! \n\n";

    echo "Return Data: \n";
    foreach($data as $key => $each_data){
        echo $key . "=" . $each_data["brand"] . "\n";
    }
}
  
  

// *******
// SELECT USING RAW QUERY
// *******

$raw_sql_query = "SELECT * FROM `shoes` ORDER BY `id` DESC";

$query = $db->execute($raw_sql_query);

if($query === false) echo "Failed";
else {
    echo "Success! \n\n";

    echo "Return Data: \n";
    foreach($query->get() as $key => $each_data){
        echo $key . "=" . $each_data["brand"] . "\n";
    }
}

  

// *******
// OTHERS RAW QUERY
// *******

$raw_sql_query = "INSERT INTO `shoes` (`brand`, `price`) VALUES ('Adidas', 432.43)";

if($db->execute($raw_sql_query) === false) echo "Failed";
else echo "Success!";

 

 

Support me

If you find this package helps you, kindly support me by donating some BNB (BSC) to the address below.

 

0x364d8eA5E7a4ce97e89f7b2cb7198d6d5DFe0aCe

 

 

 

License

The MIT License (MIT). Please see License File for more information.