Database Quick Start: Example Code

    The following code loads and initializes the database class based onyour configuration settings:

    Once loaded the class is ready to be used as described below.

    Standard Query With Multiple Results (Object Version)

    1. $query = $db->query('SELECT name, title, email FROM my_table');
    2. $results = $query->getResult();
    3. foreach ($results as $row)
    4. {
    5. echo $row->title;
    6. echo $row->name;
    7. echo $row->email;
    8. }
    9.  
    10. echo 'Total Results: ' . count($results);

    The above getResult() function returns an array of objects. Example:$row->title

    The above getResultArray() function returns an array of standard arrayindexes. Example: $row[‘title’]

    Standard Query With Single Result

    1. $query = $db->query('SELECT name FROM my_table LIMIT 1');
    2. $row = $query->getRow();

    The above getRowArray() function returns an array. Example:$row[‘name’]

    Standard Insert

    1. $sql = "INSERT INTO mytable (title, name) VALUES (".$db->escape($title).", ".$db->escape($name).")";
    2. $db->query($sql);
    3. echo $db->affectedRows();

    The Query Builder Pattern gives you a simplifiedmeans of retrieving data:

    Query Builder Insert

    1. $data = [
    2. 'title' => $title,
    3. 'name' => $name,
    4. 'date' => $date
    5. ];
    6.  
    7. $db->table('mytable')->insert($data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')