We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27733
    • 99 Posts
    I have been developing a mysql/php db app for editing a relatively large research database for a client whose main site is managed via modx. The app comprises a handful of php pages, includes, etc. and its db is on the same host as the modx db. Now, I’d like to move this app into a module so the client can edit the data from within the modx environment, based on their modx creds. I’d like to do this by using the modx dbapi only to make the connection to this external db and not have to use the dbapi on all of the subsequent mysql calls. Is this possible? In other words, roughly:
    In my config file, make the connection:
    $dbname = 'mydb';
    $link = (modx code to get conn obj);
    mysql_select_db($dbname, $link);
    

    Subsequently, make queries to the db the way you would if modx were not involved:
    $query = "SELECT ...";
    $result = mysql_query($query);
    etc.

    The general lay of the land is:
    [tt]
    /assets/modules/mydbmod
    /inc
    config.php
    header.php
    footer.php
    start.php (an include of this file is the only code in the module’s setup page)
    page1.php
    page2.php
    page3.php
    page4.php
    ...

    [/tt]
    What’s happening with the couple of methods I’ve found on the Wiki is the connection does not work on any page other than the start.php page. Once you navigate to any other page, you get an error because the user/pwd is not carried through even though the connection code is included in every page. I get the feeling the solution is simple, but I just have not been able to grab it! Any insight appreciated smiley
      ------
      Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 27733
        • 99 Posts
        Thanks for the reply! I’ve actually already gone through your article a couple times. However, I’m trying to achieve this without AJAX and modx dbapi calls on my queries. Any other angle you’ve got?
          ------
          Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          It’s all done in a frame, and not processed through the parser, so you should just be able to include your existing files in the module code. Just make sure to keep track of the module ID so you can be returned to the correct module when processing the form.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 10449
            • 956 Posts
            so the client can edit the data from within the modx environment, based on their modx creds

            If you just include() external PHP files in a module, modx-authentication won’t work, i.e. your app is unprotected (security by obscurity is not recommended).

            If you want to make sure only manager-users can run the module, you should add something on top of your files (or before you include() it in the module-config textarea).
            if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
            	exit('This file can not be accessed directly.');
            }
            if(IN_MANAGER_MODE!="true") die("Please use the MODx Manager.");