1. Fabrik 3.8 has been released. As usual, we strongly recommend that you backup your site (using Akeeba) before upgrading. Report any issues in the forums, we will answer promptly.

Opening connections to other than the Joomla DB

Discussion in 'Standard Support' started by mccarty, Mar 18, 2010.

  1. mccarty

    mccarty New Member

    Level: Community

    I am storing the data for an application in a separate database and have a fabrik connection defined for it. I want to do a variety of operations to pull certain fields from the DB as part of some server-side validation, but of course I cannot just use "$db =& JFactory::getDBO();" to access it.

    Is there an existing method within the Fabrik environment to open a DB connection for other than the Joomla DB without having to retrieve the DB uid/password from the _fabrik_connections table (or elsewhere) myself and then calling mysql_connect() directly?

    (If this doesn't exist, it would be nice to have in the toolbox... :) )

  2. rob

    rob Administrator Staff Member

    Level: Community

    $options        = array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );
     $dbs[$cn->id] =& JDatabase::getInstance( $options );
    to get an idea of the values for $driver etc you can load in J's default config values:


    $conf =& JFactory::getConfig();
          $host         = $conf->getValue('config.host');
          $user         = $conf->getValue('config.user');
          $password     = $conf->getValue('config.password');
          $database    = $conf->getValue('config.db');
          $prefix     = $conf->getValue('config.dbprefix');
          $driver     = $conf->getValue('config.dbtype');
          $debug         = $conf->getValue('config.debug');
  3. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    So where does $cn->id come from?

    -- hugh
  4. mccarty

    mccarty New Member

    Level: Community
    I probably should have been a bit clearer with my question. I am already using JDatabase::getInstance - I just reflexively wrote mysql_connect() out of habit.

    The question really was how can I get at the connection parameters for a database stored in Fabrik's connection table? I know I can do a query myself (and I have, as a workaround), but it seems to me that it would be a good thing to be able to access it through a Fabrik-supplied interface instead.

    To be more specific - is there an internal Fabrik interface I can use instead of having to use the following code I wrote to handle this:

    function mwGetFabrikDBInfo($dbname)
    $db = JFactory::getDBO();

    $query = "
    SELECT ".$db->nameQuote('host').",
    FROM ".$db->nameQuote('#__fabrik_connections')."
    WHERE ".$db->nameQuote('database')." = ".$db->quote($dbname).";

    $result = $db->loadObject();

    if ($db->getErrorNum()) {
    jexit('(mwGetFabrikDBInfo) Database query Error: ' . $db->getErrorMsg() );

    return ($result);

    function mwOpenFabrikDB($dbname, $host, $user, $password)
    $parms = array();

    $parms['driver'] = 'mysql';
    $parms['host'] = $host;
    $parms['user'] = $user;
    $parms['password'] = $password;
    $parms['database'] = $dbname;
    $parms['prefix'] = '';
    $db =& JDatabase::getInstance($parms);

    if (JError::isError($db)) {
    jexit('(mwOpenFabrikDB) JDatabase::getInstance: Database open Error: ' . $db->toString() );

    return ($db);

    $dbinfo = mwGetFabrikDBInfo($dbname);
    $db = mwOpenFabrikDB($dbname, $dbinfo->host, $dbinfo->user, $dbinfo->password);

    Best regards,

  5. amilks

    amilks Active Member

    Level: Community
    Friendly Bump...
  6. amilks

    amilks Active Member

    Level: Community
    Hugh, a friendly bump.... We talked about this on skype
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    I'm without power at my house at the moment (lightning storm last night), sitting at a coffee shop. So I don't have access to my test server to look at the code. Bump this again tomorrow.

    -- hugh
  8. amilks

    amilks Active Member

    Level: Community
    LOL! :eek: Will do
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Testing some code, will hopefully get back to you today on this one.

    -- hugh
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Try this:

    $cn = JModel::getInstance('Connection', 'FabrikModel');
    $db =& $cn->getDb();
    -- hugh
  11. amilks

    amilks Active Member

    Level: Community
    Here is the exact code I am using:


    Code (Text):
    class userAjax {
    function getStudent(){
    $cn = JModel::getInstance('Connection', 'FabrikModel');
    $db =& $cn->getDb();
    $checkId = JRequest::getVar('idField', '');
    $query = "SELECT * from jos_fbkstudents WHERE student_id = '$checkId' LIMIT 1";
    $results = $db->loadObjectList();
    echo json_encode($results);

    Code (Text):
    function doCalc(){
    var total = 0;
    var type = form_3.formElements.get('jos_signups___signup_type').getValue();
    var question = form_3.formElements.get('jos_signups___current_student_q').getValue(); //question
    var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax';
    var idField = $('jos_signups___student_id').getValue(); //id field
    var originalId = $('jos_signups___student_id'); //id field
    var verifyIdField = $('jos_signups___verify_id'); //verify id field
    var updateEmail = $('jos_signups___email'); //student email
    var updateName = $('jos_signups___name'); //student name
    var updateAddress1 = $('jos_signups___address1'); //street address
    var updateAddress2 = $('jos_signups___address2'); //street address 2
    var updateCity = $('jos_signups___city'); //city
    var updateState = $('jos_signups___state'); //state
    var updateZip = $('jos_signups___zip'); //zip
    var updateCountry = $('jos_signups___country'); //country
    var updatePrePhone = $('jos_signups___pre_phone'); //preferred phone
    var updateAltPhone = $('jos_signups___alt_phone') //alternate phone
    var updateFax = $('jos_signups___fax'); //fax
    var date1 = $('jos_signups___date1');
    var specialDate = form_3.formElements.get('jos_signups___datespecial').getValue();

    new Ajax(url,{

    var student_info = Json.decode(r);
    verifyIdField.value = student_info[0].student_id;
    updateEmail.value = student_info[0].email;
    updateName.value = student_info[0].name;
    updateAddress1.value = student_info[0].address1;
    updateAddress2.value = student_info[0].address2;
    updateCity.value = student_info[0].city;
    updateState.value = student_info[0].state;
    updateZip.value = student_info[0].zip;
    updateCountry.value = student_info[0].country;
    updatePrePhone.value = student_info[0].pre_phone;
    updateAltPhone.value = student_info[0].alt_phone;
    updateFax.value = student_info[0].fax;
    if (type == "1") {
    $('jos_signups___cost').value = 400;  
    if (question == "2" && originalId.value == verifyIdField.getValue() && originalId.value != '') {
    $('jos_signups___cost').value = 199;
    $('jos_signups___paypal_desc').value = 'Total Terminology Course, ' + specialDate + ', student id ' + originalId.getValue();
    date1.value = specialDate;
    else {  
    $('jos_signups___cost').value = 400;
    $('jos_signups___paypal_desc').value = 'Total Terminology Course, ' + date1.getValue();
    else if (type == "2") {
        total += i.getValue().toInt();
         $('jos_signups___cost').value = total;
         if ($('jos_signups___cost').value <= 1200) {
           $('jos_signups___cost').value = 1200;
         else {
         $('jos_signups___cost').value = total;
    And here is the error I am getting:

    Code (Text):
    missing ) in parenthetical
    Line 255
    Hope this helps. Maybe I am just doing something a LITTLE wrong.
    1 person likes this.

Share This Page