Database : Select (CRUD)

  • select
    The select method directive is the top level sql setter method for selecting data from database fields. It defines sql select queries that can be executed later.

    Method 1 : select
      $db->select('* from users');
                            


    Method 2 : select with chained controller
      $db->select('* from users')->where(['username'=>'Felix']);
                            
    The method above is used to set simple sql queries. Method where() should only be chained once on the select() operator in the predefined order.
    Example 1 : Executing predefined query
      $db->select('* from users')->where(['username'=>'Felix']);
    
      $result = $db->read();
                            
    Reading from database comes with different formats as the read() method is capable of applying limits to queries. Another method results() can be used to obtain and remodify data obtained from the database. Let's take a look at some few examples.

    Example 2 (Reading with limit)
      $db->query('select * from users')->read(1);
                            
    In Example 2 above, only one data is fetched from the database

    Example 3 (Reading with limits)
      $db->query('select * from users')->read(1, 2);
      $db->query('select * from users')->read(5, 7);
                            
    In Example 3 above, only two data is fetched from the database table.

    The first query translates that the number 1 data on the database should be ignored whilst 2 data should be obtained after.
    The second query translates that the number 5 data on the database should be ignored whilst 2 data should be obtained after (i.e. 7).
    Notice: It is worth noting that data obtained are always in a multi-dimentional array format.

  • results
    This method is applied on data obtained from the database. It performs a data modifier function.

    Example 4 : Data Fetching
      $db->query('select * from users where username = ?',['Felix']);
      
      $result1 = $db->read()? $db->results() : [];
      
      $result2 = $db->read()? $db->results(0) : [];
      
      $result3 = $db->read()? $db->results(0, 'username') : [];
                            
    When dealing with multi-dimentional arrays, data can be easily fetched and accessed

    Assuming we have a data returned where :

    $result1 obtained above translates [0 => ['username'=>Felix]],
    then : $result2 translates that the zero 0 key should be fetched, hence :
    $result2 will translate as ['username'=>Felix]
    $result3 will translate as Felix

    Note : In this manner, we are able to pull data easily from the data obtained. If such key does not exist, $result2 or $result3 will be set as an empty value.

    Example 5 : Data Trimming
      $db->query('select * from users where username = ?',['Felix'])->read(50);
      
      $six_results = $db->results(':6');
      
      $ten_results = $db->results(':10');
                            
    When dealing with multiple results, data obtained can be further trimmed into a specific number of items arrays. The first result obtained above i.e $six_results will only pull 6 results out of 50 results obtained or less depending on the total number of results obtained. The second result obtained above i.e $ten_results will only pull 10 results out of 50 results obtained or less depending on the total number of results obtained.

    Note : In this manner, we are able to trim data easily from the data obtained.

    Example 5 : Data Shuffling
      $db->query('select * from users where username = ?',['Felix'])->read(50);
    
      $results1 = $db->results(':shuffle');
    
      $results2 = $db->results(':10', ':shuffle');
                            
    Data obtained can be shuffled. This means that every time a data is obtained, it is shuffled or reshuffled. In the example above, 50 results may be obtained and then reshuffled which changes the position of data obtained at each reload.

    In $results2 above, 10 data was pulled out of 50 data and then reshuffled. Notice the shift in position of :shuffle directive when two arguments are supplied.