News arrow Magazine arrow Reviews arrow MOS 4.5 Database Class
MOS 4.5 Database Class Print E-mail
Written by Arthur Konze   
Friday, 25 July 2003

Mambo Open Source version 4.5 comes with a new and modified database engine. Therefore all developers should change their old scripts. As I develop myself I checked the new database class for you. Here is a small introduction how it will work on MOS 4.5:

Preparing the database query

The first thing is always to compile the SQL command. MOS 4.5 comes with a handy command which lets you compile your database query easy. The function setQuery of the database class prepares your query. You don't have to worry about table prefixes, the function will handle this for you if you use the substitute #__. The complete command looks like this:

$database->setQuery("SELECT id, title, sectionid, catid FROM #__content");

Performing the database query

To finaly execute the database query and maybe get back the results of your query, MOS comes with different functions, whether you await none, one or multiple results. Lets have a look:

Query without result
In many cases the developer don't await any return data. Mostly this is when writing to the database (sql: insert, update, delete). For this case the database class comes with a function which will simple execute that query. This function is simply called query. The function will return false if the query went wrong. Lets take a look at the example:

$database->setQuery("DELETE FROM #__categories WHERE id IN ($cids)");
if (!$database->query()) {
  echo "Error!";
}

While the first row prepares the query, the second line executes it and check's if everything went ok. If not, an error message is posted.

Query with one result
Sometimes you can be sure to have only one result returned from a query. MOS has a special function for this case which is called loadResult. This function executes the query and returns the result in a variable. Here is a small example:  

$database->setQuery("SELECT sum(hits) as count from #__stats_agents");
$hits = $database->loadResult();
echo "HITS: $hits";

Query with multiple results
If you await multiple results from your query, MOS database class comes with a fine oop function called loadObjectList. This function will perform the query and store the results in an array. The elements of the array now can easily be accessed by a foreach statement. An example:

$database->setQuery( "SELECT id, title FROM #__content");
$rows = $database->loadObjectList();
foreach($allrows as $singlerow) {
  echo "ID: $singlerow->id, TITLE: $singlerow->title
";
}

The function will return NULL if the query fails. By default the array is indexed sequential, but you can hand over a key, which will then be used for indexing.

No Warranty

Surely you understand that I can give you no warranty of the code examples until the final version is released!

 
Tag Cloud

announced   another   backend   beta   bug   category   code   community   component   components   core   database   development   directory   display   downloaded   editor   forums   grab   links   mambo   mamboportal   mambot   manager   module   modules   mos   official   phil   preview   project   robert   simpleboard   source   template   templates   upcoming   update   using   working   451  

Created with AkoCloud 1.1 final.