My favorites | Sign in
Logo
                
Search
for
Updated Aug 29, 2008 by possible248
Labels: Phase-Implementation
QueryingDatabases  
Describes how to query the databases in BurstCMS.

Introduction

Modules are usually passed the database class. The same with components. This wikipage assumes that the module and/or component already has the database class in the $db variable.

Connecting to the database

This section is short and sweet. :D

<?php
$db->connect ($host, $username, $password, $database);
?>

Queries

Creating queries

The following code creates a query:

<?php
$result = $db->query ('SELECT * FROM database');
?>

$db->query will return a query object from which you can retrieve query results and other statistics from.

Fetching results

Query objects have various class methods that allow the retrieval of results. The below example demonstrates how to get all the information that query classes provide:

<?php
// Return rows that were affected by the query.
$val = $result->affectedRows ();

// Return the number of rows that the query returned.
$val = $result->numRows ();

// Get an object containing one row of the query results.
$val = $result->fetchObject ();

// Choose to get a specific row. This parameter also works with fetchRow (); and
// fetchArray ();
$val = $result->fetchObject (3);

// Get an array with numerical keys
$val = $result->fetchRow ();

// Get an array with only associative keys. The behavior of this function is similar to
// mysql_fetch_assoc ();
$val = $result->fetchArray ();

// Returns the number of fields in the result set.
$val = $result->numberFields ();

// Return a field name given an integer offset.
$val = $result->fieldName (2);
?>

Escaping strings to enter in queries

The class method $db->escape ($string); will happily return a string escaped with a string escaping function typically provided by the database extension.

Error handling

BurstCMS database classes have powerful error handling capabilities.

<?php
// Get the last error that occurred.
$db->getLastError ();

// Get the second latest error. Default offset is 1.
$db->getLastError (2);

// Return an array containing all the errors during the life of the connection.
$db->getAllErrors ();

?>

Database classes use the exception BurstException.


Comment by k00pa.pelikoira, Aug 29, 2008

Holy shit! This is so simple!


Sign in to add a comment
Hosted by Google Code