We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18913
    • 654 Posts
    I’ve looked at the docs for the database API and hoped someone could clear something up for me. In particular, I’m trying to understand if there is any risk to the operation of a MODx-based site if one is also using the db calls to access a non-MODx MySQL database? My impression was that a connection to the MODx database was always open and that to access the non-MODx database, this would need to be closed and a new connection opened up. While operations are performed on that non-MODx database, is the MODx database inaccessible - and if so, is it just for that session?

    I’m trying to figure out if interacting with non-MODx databases via the API will bust anything and am hoping someone with more in-depth familiarity with all this can set me straight.
    TIA,
    MattC
      • 22303 MODX Staff
      • 10,725 Posts
      You cannot close the MODx connection or you will cause problems. You need to create a new, unique connection to another DB. I believe this can be done by creating a separate, unique instance of the DBAPI class.
        • 18913
        • 654 Posts
        Hi,
        Thanks for the reply. Sounds like I am right to avoid the API functions in that case. What has me confused a bit is this example from the wiki :

        //connect to seperate mysql db
        $modx->db->connect('123.45.6.7', 'mydb', 'user', 'password', true);
        $res = $modx->db->select('*', 'this_table');
        while($tmp = $modx->db->getRow($res, 'assoc'))
        {
           //do somethign with the data
        }
        //disconnect
        $modx->db->disconnect()
         
        //reconnect to modx db
        $modx->db->connect();


        which to me suggests that it’s possible to link with another db without impacting modx. But it’s not clear that in this example another instance (as you suggested) is automatically being created, as there is an explicit reconnect to the modx db at the end. This, despite what the text preceding the example seems to imply.

        If I’m wrong, that would be good to know, as I wouldn’t then have to recreate a pile of db access calls smiley

        Thanks again,
        MattC