IntroductionModules 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 databaseThis section is short and sweet. :D <?php
$db->connect ($host, $username, $password, $database);
?> QueriesCreating queriesThe 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 resultsQuery 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 queriesThe class method $db->escape ($string); will happily return a string escaped with a string escaping function typically provided by the database extension. Error handlingBurstCMS 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.
|
Holy shit! This is so simple!