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

Connecting to another MySQL DB from within a plugin

Development and Programming

Sean Delaney's avatar
Sean Delaney
64 posts
14 years ago
Sean Delaney's avatar Sean Delaney

I need to connect to another MySQL database on the same server as my EE database.

How can I do this from within my plugin?

I have tried using mysql_connect() which worked fine but I got out of memory errors when trying to selecting the DB using mysql_select_db(). I even bumped up my PHP memory to 4GB but still didnt work.

I have also tried calling: $this->load->database(‘xxxx’); (Core CI database call) from within my plugin but I get ‘call to member function database() is a non object….’

How can I connect and select another MySQL DB from a plugin?

Thanks

Sean

       
Click Rain 1's avatar
Click Rain 1
9 posts
14 years ago
Click Rain 1's avatar Click Rain 1

Sean -

Here’s a copy of the __construct() from a test plugin I threw together to do what you’re looking for. I’ve tested it and it seems to work:

public function __construct()
    {
        $this->EE =& get_instance();
        
        $this->alt_db = mysql_connect($this->EE->db->hostname,$this->EE->db->username,$this->EE->db->password);
        mysql_select_db('pi_test_db',$this->alt_db);
        
        $out = array();
        $q = mysql_query("SELECT * FROM `pi_test_tab`;",$this->alt_db);
        while ($row = mysql_fetch_assoc($q))
        {
            $out[$row];
        }
        
        $this->return_data = print_r($out,TRUE);
    }

Is it the best way to do it? Probably not. But it works.

       
Sean Delaney's avatar
Sean Delaney
64 posts
14 years ago
Sean Delaney's avatar Sean Delaney

Hi Mark,

Thank you so much for the code snippet. It worked first time after I swapped your example details with mine…

Sean

       
fostermade's avatar
fostermade
97 posts
14 years ago
fostermade's avatar fostermade

You could also try:

$this->EE = get_instance();
$this->EE->load->database(‘xxx’);
       
Sean Delaney's avatar
Sean Delaney
64 posts
14 years ago
Sean Delaney's avatar Sean Delaney

Thanks for your input also Shawn! Always good to have more example. 😊

       
nathanpitman's avatar
nathanpitman
531 posts
14 years ago
nathanpitman's avatar nathanpitman
You could also try:
$this->EE = get_instance();
$this->EE->load->database(‘xxx’);

Interested, would this trump the default EE loaded DB or create a new DB object which can be queried alongside the standard EE DB connection? 😊

       
eurotrash's avatar
eurotrash
7 posts
14 years ago
eurotrash's avatar eurotrash

http://ellislab.com/forums/viewthread/195522/

Works for me. Helps if you are familiar with Code ignitor.

       
Sean Delaney's avatar
Sean Delaney
64 posts
14 years ago
Sean Delaney's avatar Sean Delaney
You could also try:
$this->EE = get_instance();
$this->EE->load->database(‘xxx’);
Interested, would this trump the default EE loaded DB or create a new DB object which can be queried alongside the standard EE DB connection? 😊

Correct, it won’t trump the default DB instance as we are selecting another DB object.

       

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.