PHP code example of keysmash / php-easy-db

1. Go to this page and download the library: Download keysmash/php-easy-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/ */

    

keysmash / php-easy-db example snippets


// 1 for mysql
$db = new Database(1, "localhost", "foobar", "root", "toor");
// 2 for postgres
$db = new Database(2, "localhost", "foobar", "root", "toor");

class Test extends DatabaseTable {
  protected static $table_name = "test";
  protected static $db_fields = array(
    'id',
    'test'
  );
  protected static $db_types = array(
    'int(11) NOT NULL',         // id
    'varchar(11) NOT NULL'     // test
  );
  
  public $id;
  public $test;
  
  public static function find_by_test($database, $test){
    $sql = "SELECT * FROM " . static::$table_name . " WHERE test=" . $test;
    $result_array = static::find_by_sql($database, $sql);
    return !empty($result_array) ? $result_array : false;
  }
}

$row = Test::find_by_id($db, 1);

$rows = Test::find_by_test($db, "foobar");

$rows = Test::find_by_sql($db, "SELECT * FROM test LIMIT 3 ORDER BY ASC");

$rows = Test::find_all($db);

foreach($rows as $row){
  // do stuff
}

echo $row->test;

$row->test = "foobar";

if($row->save()){
  echo "yeah, it worked!";
} else {
  echo "dang it";
}

if($row->delete()){
  echo "yeah, it worked!";
} else {
  echo "dang it";
}