Skip to main content
Topic: new db style (Read 6313 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

new db style

long ago I wanted to offer new style of access to a database

Example.

old style
Code: [Select]
<?php
# ...

    $db = database();

    # ...

    // Grab the categories sorted by cat_order.
    $request = $db->query('', '
        SELECT id_cat, cat_order
        FROM {db_prefix}categories
        ORDER BY cat_order',
        array(
        )
    );
    while ($row = $db->fetch_assoc($request))
    {
            # ...
            $cat_order[$row['id_cat']] = $row['cat_order'];
    }
    $db->free_result($request);

    # ...
   
new style
Code: [Select]
<?php

    use Elkarte\DB;

    # ...

        // Grab the categories sorted by cat_order.
        $q = DB::query('
            SELECT id_cat, cat_order
            FROM {db_prefix}categories
            ORDER BY cat_order',
            []
        );
        while ($row = $q->fetch_assoc) {
            # ...
            $cat_order[$row['id_cat']] = $row['cat_order'];
        }
        $q->free;

        # ...

DB class here
Last Edit: December 20, 2013, 04:27:50 am by Inter
Sorry for my English

Re: new db style

Reply #1

If I understand it correctly the use of Closure means PHP > 5.3, right?
Bugs creator.
Features destroyer.
Template killer.

Re: new db style

Reply #2

it is possible to make on another:
is_callable (...

the main idea in that that was easier to perceive a code (probably I am not right therefore any judgement is interesting to me)
Sorry for my English

Re: new db style

Reply #3

ohhh.. now I think I see what you mean (was paying attention to other details and not the main one).

It is indeed easier to read and I thought about something like that from time to time, dunno about the fact that it has to re-instantiate the class for each and every query, seems a lot of overhead, but code design is not my field, so I don't know.
Bugs creator.
Features destroyer.
Template killer.

Re: new db style

Reply #4

the class completely isn't completed - there shall be still methods
I simply showed - someone can will want to use it
If someone has ideas - will be glad to look
Sorry for my English