There are times when you may need to grab data from a database outside the Zencart database, it may be the case when two zencart installation need to share data or may be some other reasons. To achieve this, you need to open 2 database connections that wont overlap each others. People generally open the connection with raw mysql_connect and work with that. There are situations when people open numbers of connections whenever needed which a total mess. To solve all these issues here is my solution that is best suited for Zencart.
Zencart has its own query class “queryFactory“. The database initialization takes place in the following file each time a page loads CATALOG/includes/init_includes/init_database.php
After initialization is done, $db is the connection object that is used throughout the system.
So you need another connection object like $db, lets call it $new_db, that will use a different database to grab data. For creating the new connection follow the following steps:
- Open a new file and save it as
CATALOG/includes/extra_configures/new_db_config.php. The file name can be anything. This config file will contain detail of the new connection. Add the following definitions in the file:<?php define('NEW_DB_SERVER', 'new_db_server'); //it may be anywhere, generally localhost define('NEW_DB_USERNAME', 'new_db_username'); define('NEW_DB_PASSWORD', 'new_db_password'); define('NEW_DB_DATABASE', 'new_database_name'); ?>Save the file.
-
Open the database init file
CATALOG/includes/init_includes/init_database.php.
Add the following lines at the bottom of the file, just before the ?> .if(defined('NEW_DB_SERVER')){ $new_db = new queryFactory(); if(!$new_db->connect(NEW_DB_SERVER, NEW_DB_USERNAME, NEW_DB_PASSWORD, NEW_DB_DATABASE, USE_PCONNECT, false)) { die('Unable to connect new database. Check config in extra_configures/new_db_config.php'); } }Dont save it yet. Save the file as
CATALOG/includes/init_includes/overrides/init_database.php. This way, the original files wont be overwritten.
So your new connection is ready for the action. Right now you have 2 different connections connecting to 2 different databases. $db is the default connection used by zencart through out the system. $new_db is your connection you can use whenever you need.
If at any point you need to grab some data from the other database, just use the new connection variable $new_db instead of $db.
For example, $new_db->Execute("select ANYTHING from ANYTABLE_IN_NEW_DB");
Just remember, $new_db cannot perform any operation on the default database. You can use the new connection object just like the original connection object and use all the functions the original connection uses.
Let me know if you have any questions.


#1 by naveen on December 23rd, 2009
its is working for database related functions from the fronten , but its not working for admin .i have done the same procedure for admin as well but zencart fails to create categories , products after doing the above procedure.
Do u have any suggestions for admin side?
#2 by imran on December 24th, 2009
Hi Naveen,
It should work for the admin as well. Are you saying regular products, categories cannot be created after adding the new database connection?