-
Notifications
You must be signed in to change notification settings - Fork 0
Database
If your app works with a MySQL database, please turn on the provided extension at /app/config.php file on line 15.
To set your database charset encoding please use the set_encoding("str") function on the /app/start.php file.
$DB->set_encoding('utf8')
You will be able to use it on each controller you make.
$this->db->make('select','table');
You can select the entire table, or a specific row(/s). Use the where() function to add parameters to the select function (and insert, delete functions).
To select specific row/s instead of all (*), add a third parameter to the function. $this->db->make('select', 'table', 'what');
The where() function will contain an array with what you want to select.
$this->db->where(array(
"id" => "3"
));
You can list the results in your desired order by using the order() function:
$this->db->order('id', 'DESC');
The first parameter is the key which will be filtered, and the 2nd parameter is optional to change the order of results (DESC for descending, and ASC for ascending).
$this->db->make('insert', array(
'table' => 'table',
'name' => '123test',
'color' => 'blue',
));
The table parameter will specify which table you are about to insert the row, and all other array keys will contain the column values.
$this->db->make('delete', 'table');
$this->db->make('update', array(
'table' => 'table',
'name' => 'Daniel Golub'
));
Similar to the insert() channel, the table value will contain the table you are about to update, and all other array keys will contain the column values. You may use the where() function here to filter the results.
$this->db->limit(5)
To limit the results use the limit function.
$this->db->append($str)
To append text to the query string, use the append function. Pass a string.
To duplicate a row in the table, use the following code:
$this->db->make('duplicate', 'table')
$this->db->execute();
This function will execute your command. You can add a parameter to get a different type of result:
- query (default value) - will return just the query results
$this->DB->execute()
- boolean - will return a boolean value if the query succeed (true or false)
$this->DB->execute('boolean')
- rows - will return the total rows number
$this->DB->execute('rows')
- fetch - will return an array which will contain the data of the row which was founded
$this->DB->execute('fetch')
- loop - will return an array which will contain the data of ALL THE ROWS which were founded. Inside each array (row) will be their data, like in the fetch option.
$this->DB->execute('loop')
To get the latest command which was used, please use the following code:
$return = $this->db->getLastCommand();
echo $return;
You can get the structure of a table by the following code:
$return = $this->db->getLastCommand();
echo $return;
$return will contain column_1 | column_2 | column_3
etc. You can make an array of the results by using the following code:
$return = $this->db->getLastCommand();
$return = explode(" | ", $return);
print_r($return);
To count how many queries have been used, please use the following code:
$return = $this->db->total();
echo $return;
You can specify a parameter to change the returned value:
- total (default) - count all the queries you have used
$return = $this->db->total();
- categories - returns an array with each action type (SELECT, INSERT, DELETE, UPDATE) and for each counts query
$this->db->total('categories')
Happy coding :)