CRUDités is an extension to the popular PHP ezSQL class by Justin Vincent. Like ezSQL it aims to simplify all common operations between PHP and SQL databases. In particular, CRUDités works on MySQL databases, the most commonly used database around the web.
Quoting Wikipedia
Crudités are traditional French appetizers comprising sliced or whole raw vegetables which are dipped in a vinaigrette or another dipping sauce. Crudités often include celery sticks, carrot sticks, bell pepper strips, broccoli, cauliflower, and asparagus spears.
As of its name, CRUDités is just a set of handy methods to quickly manage all your operations of writing, storing and deleting from and to MySQL databases.
CRUDités, aside from the basic ezSQL methods (see here for details) adds some methods to build MySQL queries from strings and data arrays, new methods to retrieve datas from columns and rows, some utility methods and a whole new table binding system, to store table's fields and quickly update and store new datas. See next chapter for a quick introduction to the main CRUDités features.
Like ezSQL, CRUDités needs to be instantiated. It accepts all the basic ezSQL arguments (username, password, database name, host), plus an optional prefix.
The prefix may be a string that defines a common prefix for all tables in the database and it's a usefull paramenter since you can write your queries pointing to #__my_table leaving CRUDités the dirty work to replace "#__" with passed in prefix. This way if your tables' prefix will change you won't have to check all of your queries for changes.
To instantiate CRUDités first remember to include the class file and the two ezSQL dependency files, then create a new instance:
require("ez_sql_core.php");
require("ez_sql_mysql.php");
require("class.crudites.php");
$db = new CRUDites("dbuser","dbpassword","dbname","localhost");
Now that a new CRUDités instance has been created, you can work with it to generate a select query and get the resulting rows as an array of objects:
$query = $db->select_query(array(
'select' => 'id,name,surname',
'from' => 'customer_names',
));
$rows = $db->get_results($query); //standard ezSQL method
CRUDités also provides some quicker methods, so that the previous code could be written like this:
$rows = $db->get_data(array(
'select' => 'id,name,surname',
'from' => 'customer_names',
));
An handy feature in CRUDités is that all of its query-related methods return false on error or when no results are found.
if ($rows) {
echo "Rows found!";
} else {
echo "Nothing to show in here!";
}
Another cool feature of CRUDités is table binding.
CRUDités maintains an index of database tables as an multidimensional associative array in which keys are table names and values are arrays containing a reference to each column's name and to the defined primary key field.
By using table binding you can quickly switch from table to table and work much more faster on it.
To add a table to the index, just use the add_table() method:
$db->add_table(
'my_table', //table's name
array( //fields list
'id',
'name',
'surname'
),
'id' //primary key
);
This way a table named my_table has been added to the index, with a reference to its columns and a primary key set to the id field.
You can select this newly added table by using the bind() method and work on it with CRUDités specific methods:
$db->bind('my_table'); // select the table
$rows = $db->select_rows(); //get all rows as an array of objects
$new_data = array ("name" => "John", "surname"=>"Doe");
$db->store($new_data); // store a new row inside the table
If you find CRUDités interesting, check out the complete documentation and download the complete package from here.
CRUDités is released under a dual licence: MIT and GPL2.
This documentation was generated by phpDocumentor v1.4.2