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
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.
You could also try: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? 😊$this->EE = get_instance(); $this->EE->load->database(‘xxx’);
Correct, it won’t trump the default DB instance as we are selecting another DB object.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.