PHP code example of nhrdev / nhr_db

1. Go to this page and download the library: Download nhrdev/nhr_db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nhrdev / nhr_db example snippets


use NhrDev\NHR_DB\DB;

$db = new DB(`DB_USERNAME`, `DB_PASSWORD`, `DB_NAME`, [`DB_HOST`, `DB_PORT`, `DB_CHARACTERSET`]);

$db->disconnect();

$db->connect();

if($db->is_connected()){
    echo "Connected!";
}

$table = $db->table( 'table_name' );

$table->id()
      ->int( 'id', 255 )
      ->unsigned_int( 'id', 255 )
      ->unsigned_bigint( 'id', 255 )
      ->col( 'id', DB::int(1), true )

      ->str( 'username', 255)
      ->col( 'username', DB::str(100) )

      ->text( 'details' )
      ->col( 'details', DB::text() )

      ->float( 'amount' )
      ->col( 'amount', DB::float() )

      ->enum( ['true', 'false'] )
      ->col( 'enum', DB::enum(['value1', 'value2']) )

      ->col( 'date', DB::date() )
      ->col( 'datetime', DB::datetime() )

      ->timestamp();

$table->create();

$table->add(string $name, string $type_and_length, bool $is_primary = false, $is_auto_increment = false, bool $is_not_null = false, bool $is_unique = false);

$table->drop("COLUMN_NAME");

$table->drop_all();

$table->col()->add->drop->drop_all();

$table->insert([
  'column_name' => 'value'
]);

$table->last_insert_id()

$table->update([
  'column_name' => 'value'
])->where("active", DB::eq(true))
  ->where("user", "=", "true")
  ->where("amount", DB::between(10, 2001))
  ->or("referrer", "=", -1)
  ->or("roll", DB::between(10, 59))
  ->where("username", DB::begins_like("ra"))
  ->execute();

Returns the number of rows affected or false on failure.

$table->update([
  'column_name' => 'value'
]);

$table->delete()
  ->where("active", DB::eq(true))
  ->where("user", "=", "true")
  ->where("amount", DB::between(10, 2001))
  ->or("referrer", "=", -1)
  ->or("roll", DB::between(10, 59))
  ->where("username", DB::begins_like("ra"))
  ->order_by('id', 'DESC')
  ->limit(5)
  ->execute();

Returns the number of rows affected or false on failure.

$table->delete();

$rows = $table->select([], DB::OBJ)
  ->where("active", DB::eq('true'))
  ->or("amount", DB::between(10, 500))
  ->order_by("id", 'DESC')
  ->limit(1)
  ->offset(1)
  ->execute();

$rows->all();

$rows->first();

$rows->last();

$rows->each(function($row, $index){
    # your code
}, false);

$rows->get(5);