When I worked with cms made simple the first time, I had to spend some time to learn how to operate with mysql database. This reference guide is my attempt to help those who are just beginning with cms made simple and doing the first steps in customizing this wonderful content management system. I hope this tutorial will be useful for somebody.

When you work on CMS Made Simple customization, you might need to operate with mysql database. To do this, you should call methods of database object which can be accessed through core object of cms made simple. The core object can be accessed through $gCms variable.

If you are writing a function, don’t forget to put
global $gCms;
in your function.

Then create a variable and put there a pointer to the database object:
$db =& $gCms->GetDB();

Then you can easily operate with $db using its native methods. Here are the most used methods:

$r = $db->Execute($sql);
This is the method you will use for sure. It executes the query ($sql) and returns the result object.

With result object you can do many things:
Check if query has been executed:
if ($r) {
// do something if query successed
} else {
// do something if query failed, maybe show an error message
}

Get number of records returned in result:
$r->RecordCount()

Get values of records returned in result:

$result = array();
while($r && $line = $r->FetchRow()) {
$result[] = $line;
}

With database object you can also escape strings for secure purposes. Like that:
$r = $db->Execute("SELECT * FROM auth WHERE login=". $db->qstr($login)." AND password=".$db->qstr($password));

qstr method adds slashes and makes SQL injection impossible. It’s strongly recommended to escape the values coming from remote user. It will help you to make secure and safe application.

Of course, there are more methods to be used, but these ones should be enough to perform simple customizations. If you need assistance on complex ones, you can request my help through the form on this website.