We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

use EE to access other database

Development and Programming

thisconnect.be's avatar
thisconnect.be
73 posts
17 years ago
thisconnect.be's avatar thisconnect.be

Hello all,

I’am creating a bridge between EE and our application. This application will run on the same server as EE. We are trying to access an other database on the same server but this seems to be harder then first thought.

I think i’am doing it right but from some reason when creating a new DB object the rest of page fails to render.

When i call the module by the EE tags i get the following error:

MySQL ERROR:

Error Number: 1146

Description: Table ‘geboortelijst.exp_template_groups’ doesn’t exist

Query: SELECT exp_templates.template_name, exp_templates.template_id, exp_templates.template_data, exp_templates.template_type, exp_templates.edit_date, exp_templates.save_template_file, exp_templates.cache, exp_templates.refresh, exp_templates.no_auth_bounce, exp_templates.enable_http_auth, exp_templates.allow_php, exp_templates.php_parse_location, exp_templates.hits, exp_template_groups.group_name FROM exp_template_groups, exp_templates WHERE exp_template_groups.group_id = exp_templates.group_id AND exp_template_groups.site_id = ‘1’ AND exp_templates.template_name = ‘head’ AND exp_template_groups.group_name = ‘embeds’

It seems that even though i create a new DB object EE reuses my own created object.

Is this normal or am i using the database class wrong? Any help would be much appreciated .

Here is my mod.babywishlist:

<?php

if ( ! defined('EXT'))
{
    exit('Invalid file request');
}

// -----------------------------------------
//Begin class
// -----------------------------------------

class Babywishlist
{
    var $return_data= '';
    
    // -------------------------------
    // Constructor
    // -------------------------------
    
    function Babywishlist()
    {
        global $PREFS,$TMPL,$FNS;
        $tagdata = '';
        require_once(PATH_DB.'db.'.$PREFS->ini('db_type').EXT);
        
        $db_config = array(
            'hostname'      => $PREFS->ini('db_hostname'),
            'username'      => $PREFS->ini('db_username'),
            'password'      => $PREFS->ini('db_password'),
            'database'      => "geboortelijst",
            'prefix'        => $PREFS->ini('db_prefix'),
            'conntype'      => $PREFS->ini('db_conntype'),
            'debug'            => 1,
            'show_queries'    => TRUE,
            'enable_cache'    => FALSE
        );
        
        $mydb = new DB($db_config);
         //echo $mydb->database;
         $res = $mydb->query("SELECT * from shopkinds");
         unset($mydb);
        //$my = $mydb->query("SELECT shopkindname_nl from shopkinds");
        /*
        if($query->num_rows > 0){
            foreach ($query->result as $row)
            {
                $tagdata .= $TMPL->tagdata;
                
                //pref conditional
                $cond['shopname'] = $row['shopkindname_nl'];
                $tagdata = $FNS->prep_conditionals($tagdata, $cond);
                // parse variables
                foreach ($TMPL->var_single as $key => $val)
                {
                    switch ($key)
                    {
                        case 'shopkindname_nl':
                            $tagdata = $TMPL->swap_var_single($val, $row[$key], $tagdata);
                            break;
                        case 'other_variable':
                            $tagdata = $TMPL->swap_var_single($val, $row[$key], $tagdata);
                            break;
                    }
                }
                // end parsing
            }
        }
        else 
        {
         echo "hey";    
        }
        $this->return_data = $tagdata;
        */
    }
    
    // END
}
// END CLASS
?>
       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

Try adding ‘:3306’ to the hostname and see if that helps. There is an annoying problem with the PHP to MySQL interaction where if you are using the same login information, it will not create a new connection but will use the already existing one. We discovered that including the port number got around this problem.

       
thisconnect.be's avatar
thisconnect.be
73 posts
17 years ago
thisconnect.be's avatar thisconnect.be

yeap thanks paul i foudn the solution today in the pmachine code 😊

its working now

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

Technically, if you are doing your work (with EE doing nothing during it), you could simply use the same connection and switch databases. The reason the pMachine Import code does it differently is that it needs data from both database at the same time.

       
thisconnect.be's avatar
thisconnect.be
73 posts
17 years ago
thisconnect.be's avatar thisconnect.be

Yeah i know but i didnt manage to get it working directly.

Do you have to reconnect then? Can you provide a small snippet?

Thanks,

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

Not tested, but probably something like this:

<?php

global $PREFS, $DB;

$DB->database = 'ee200';
$DB->select_db();

$query = $DB->query("SELECT * FROM exp_weblog_titles LIMIT 1");

print_r($query->result);

$DB->database = $PREFS->ini('db_name');
$DB->select_db();

?>
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.