Forums / Developer / Using external db in extension causes problems

Using external db in extension causes problems

Author Message

Atle Pedersen

Friday 15 September 2006 12:13:43 am

Hello.

I'm writing an extension where I'm fetching some data from en external database. The problem is that when my function returns, further mysql function calls will be towards this external database. This seems to confuse the rest of eZ publish, which reports several mysql query errors.

When calling mysql_connect(...) a handler is returned. The kind of solution I would prefer was to have a mysql function that returned a copy of the handler to the active database, and another function which used this handler to set the active database again. I would expect to find this in PHP, but can't seem to. Is there no such thing, or is it me that is just blind?

Given that the above solution is not possible, is there some mechanism, a function call, in eZ publish, that will reassociate the mysql function calls with the correct ez publish db?

Any suggestions?

Atle

Xavier Dutoit

Friday 15 September 2006 12:46:46 am

Hi,

I've done it with several dbs and it works (tried it from cronjobs and datatype and modules).

What I do is to close the connection on the external db

      mysql_free_result($result);
      mysql_close($this->_connectionID);

Does it solve your problem ?

X+

http://www.sydesy.com

Atle Pedersen

Friday 15 September 2006 2:47:14 am

Only closing the link/handler didn't seem to be enough in my case. What did seem to do the trick, though, was always to include the handler in every mysql function call regarding the database. It appears to me it's the mysql_select_db(...) that changes database currently in use, and thus confuses eZ. Adding the link resource here solved the problem for me.

Thanks
Atle

Edit: Actually I was a bit premature there. The problem persists. The only thing that seems to work is to make a new call to mysql_select_db(...) with the original database name. Which means I have to start reading ini files... This work when the databases are on the same server, but I'm not sure how this sollution would work if one database is on an external server.

Ɓukasz Serwatka

Friday 15 September 2006 9:40:40 am

You can connect to external DB using eZDB lib.

Here is an example:

include_once( 'lib/ezdb/classes/ezdb.php' );

$dsn = array(
	'server' 	=> 'localhost',
	'user' 		=> 'user',
	'password'	=> 'mypass',
	'database'	=>	'mydb',
	'show_errors' => true
	);

$extdb =& eZDB::instance( 'ezmysql', $dsn, true  );
$extdb->arrayQuery("SELECT * FROM EXT_TABLE");

Then using eZDB lib you may query your external DB without errors. Witn new instance you can still get access to eZ publish current db.

    include_once( 'lib/ezdb/classes/ezdb.php' );
    $db =& eZDB::instance();

See lib/ezdb/classes/ezdb.php for more details.

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

Atle Pedersen

Monday 18 September 2006 7:34:47 am

Thanks Lukasz,

getting the information about the current db instance solves my probleml. Now it's easy resetting the handler in a generic way, so that eZ doesn't get confused.

Atle

Xavier Dutoit

Monday 18 September 2006 1:24:20 pm

Hi Atte,

Not sure I got you. You mean that you added

   include_once( 'lib/ezdb/classes/ezdb.php' );
   $db =& eZDB::instance();

After your code and it works fine ?

X+

http://www.sydesy.com

Kristof Coomans

Saturday 28 October 2006 6:55:48 am

The third parameter in $extdb =& eZDB::instance( 'ezmysql', $dsn, true ); is important, it forces the creation of a new instance instead of returning the eZ publish database instance cached in $GLOBALS['eZDBGlobalInstance'];

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Atle Pedersen

Thursday 24 May 2007 6:22:33 am

Revisiting an old post.

The problem is that what I'm working with is an external system using its own database mechanisms and calls. So Lukasz Serwatka's solution above is not feasible without rewriting parts of the other system.

When the external code calls

    mysql_close();

this seems to disrupt eZ publish.

I thought I solved it using

mysql_select_db('ezdatabase');

after the other code.

However, when the external code is called more than once for a template, this also fails.

So now, based on rereading the discussion above, I've worked out the following sollution:

    $dsn = array(
             'server'        => $GLOBALS['eZDBGlobalInstance']->Server,
             'user'          => $GLOBALS['eZDBGlobalInstance']->User,
             'password'      => $GLOBALS['eZDBGlobalInstance']->Password,
             'database'      => $GLOBALS['eZDBGlobalInstance']->DB,
             'show_errors' => $GLOBALS['eZDBGlobalInstance']->RecordError
    );

//..... other code including mysql_close(); does its thing here .....

    eZDB::instance( 'ezmysql', $dsn, true );

It feels sort of hackish, and I'm not sure about using the 'RecordError' variable, but it seems to work.

Any comments or suggestions for better ways of doing this?

eZ debug

Timing: Jan 31 2025 00:23:23
Script start
Timing: Jan 31 2025 00:23:23
Module start 'content'
Timing: Jan 31 2025 00:23:23
Module end 'content'
Timing: Jan 31 2025 00:23:23
Script end

Main resources:

Total runtime0.1999 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0087 588.0625370.2734
Module start 'content' 0.00870.0206 958.33591,013.7031
Module end 'content' 0.02930.1706 1,972.03913,902.2891
Script end 0.1999  5,874.3281 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00432.1602200.0002
Check MTime0.00140.6782200.0001
Mysql Total
Database connection0.00120.610710.0012
Mysqli_queries0.101250.61671410.0007
Looping result0.00130.63561390.0000
Template Total0.170185.110.1701
Template load0.00080.379110.0008
Template processing0.169484.714210.1694
Override
Cache load0.00050.259610.0005
Sytem overhead
Fetch class attribute can translate value0.00200.996810.0020
XML
Image XML parsing0.00020.116010.0002
General
dbfile0.01417.0464200.0007
String conversion0.00000.002930.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs