Thursday, July 28, 2011

SugarCRM - Accessing Database

I wanted to do a custom query in SugarCRM and I wanted to use the database connection that was already there so I would not have to create a custom connection in case I switched databases or something.  In order to do this, it was really easy.  All I had to do was add the following line of code:

global $db;


This gave me access to the db class.  Below is an example of using it:

global $db;
$query = "SELECT now() AS thetime;";
$res = $db->query($query);
$row = $db->fetchByAssoc($res);
$TheTime = $row['thetime'];
$GLOBALS['log']->debug("time: $TheTime");


The code above will put a timestamp in the log when accessed.





No comments:

Post a Comment