Hotaru Docs


Database Class

Hotaru 1.2 introduced a new class for database functions. It sits on top of ezSQL and simplifies data retrieval. This is an alternative to Working Directly with the Database, not a replacement. You can use it like this:

$results = $h->db->select($h, $select, $table, $where, $orderby, $limit);

So if you wanted to get the top 10 posts of all time, you'd do:

$select = array('*');
$table = 'posts';
$where = array(
    'post_archived = %s' => 'N',
    'post_type = %s' => 'news',
    'post_status = %s' => 'top');
$order_by = 'post_votes_up DESC';
$limit = '10';
$results = $h->db->select($h, array('*'), $table, $where, $orderby,  $limit);

If those %s arguments are new to you, read Database prepare() Function.

Alternatively to the above code, you can fill the $h->db object like this:

$h->db->select = array('*');
$h->db->table = 'posts';
$h->db->where = array(
    'post_archived = %s' => 'N',
    'post_type = %s' => 'news',
    'post_status = %s' => 'top');
$h->db->order_by = 'post_votes_up DESC';
$h->db->limit = '10';
$results = $h->db->select($h);

Pros and Cons

One big plus for using the select function instead of writing your own queries is that the results of each query are cached by the Hotaru smartCache.

On the down side, the class is currently limited to SELECT only, and uses ezSQL's get_results function (not get_var or get_row), so if you want to retrieve an individual value or a single row, you will need to extract what you need from the returned array.

Getting StartedDesign and LayoutPlugin DevelopmentAdvanced TopicsFunction ReferenceTroubleshooting