PHP code example of aberdeener / koss

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

    

aberdeener / koss example snippets


        // Column name => Type
        $casts = array(
          'id' => 'int',
          'username' => 'string',
          'money' => 'float'
        );
        

      // Column name => Value
      $values = array(
        'username' => 'Aber'
      );
      

      // Column name => Value
      $row = array(
        'username' => 'Aberdeener',
        'first_name' => 'Tadhg',
        'last_name' => 'Boyle'
      );
      

      // Column name => New value
      $values = array(
        'username' => 'Aber'
      );
      

    // Get the "username" and "first_name" column in the "users" table, limit to only the first 5 rows, and sort by their username descending.
    $results = $koss->getSome('users', ['username', 'first_name'])->limit(5)->orderBy('username', 'DESC')->execute();
    // MySQL Output: SELECT `username`, `first_name` FROM `users` ORDER BY `username` DESC LIMIT 5
 
    // Get all columns in the "users" table, and when they're logged in, limit to only the first 5 rows.
    // Note the usage of new variable, $query in anonymous function. This will be passed by Koss.
    $results = $koss->getAll('users')->when(fn() => isset($_SESSION['logged_in']), fn(SelectQuery $query) => $query->limit(5))->execute();
    // MySQL Output: SELECT * FROM `users` LIMIT 5

    // Get the "username" column in the "users" table, but also select the "last_name" column.
    $results = $koss->getSome('users', 'username')->columns(['last_name'])->execute();
    // MySQL Output: SELECT `username`, `last_name` FROM `users`
    

    // Insert a new row into the "users" table, if there is a unique row constraint, update only the username to "Aber"
    $koss->insert('users', ['username' => 'Aberdeener', 'first_name' => 'tadhg', 'last_name' => 'boyle'])->onDuplicateKey(['username' => 'Aber'])->execute();
    // MySQL Output: INSERT INTO `users` (`username`, `first_name`, `last_name`) VALUES ('Aberdeener', 'tadhg', 'boyle') ON DUPLICATE KEY UPDATE `username` = 'Aber' 
    

    // Update any existing rows in the "users" table which match the following criteria, update the username to "Aber" and the first_name to "Tadhg" where their "id" is 1 and their last_name is "Boyle"
    $koss->update('users', ['username' => 'Aber', 'first_name' => 'Tadhg'])->where('id', 1)->where('last_name', '=', 'Boyle')->execute();
    // MySQL Output: UPDATE `users` SET `username` = 'Aber', `first_name` = 'Tadhg' WHERE `id` = '1' AND `last_name` = 'Boyle'