We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30376
    • 10 Posts
    I’m in the process of migrating a couple of sites to modx. I’ve got some custom php/mysql apps that I’d like to bring across, but I’m having some trouble. I’ve copied my php code along with mysql queries into a snippet, but I keep getting error messages like:

    "« MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource:
    « Execution of a query to the database failed - Table ’tonomud_jim.modx_site_templates’ doesn’t exist »"

    So I do some searching here, and find this wiki article discussing accessing a mysql database, and it would appear that I can’t just use plain PHP to access another MySQL database on the same server.
    http://wiki.modxcms.com/index.php/Access_another_database_from_MODx

    So....I did some messing around with my custom php/mysql code and was able to connect to the database "tonomud_jim.works" UNTIL I performed just about any other action (i.e. creating and saving a new page in the manager, etc) which interacted with the database. Then I get the error above, until I re-save/upload my php code again. Make sense?

    Any ideas as to what I can do to enable my custom code on this install of modx without having to re-write everything?

    I can post code if that will help. Thanks for any help you can provide!
      • 7231
      • 4,205 Posts
      Maybe a cache problem? Try using the snippet uncached: [!snippet!] rather than [[snippet]]
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 30376
        • 10 Posts
        thanks for the suggestion--just tried it, doesn’t seem to have fixed it.
          • 4041
          • 788 Posts
          Well, I have used the already open modx mysql connection in snippets, just use a standard mysql_query in the snippet.

          I have also used a regular mysql connect routine in a snippet to grab another database without any issues, set your connection details and then any query you want.

          $host = ""; // your db host
          $dbuser = ""; // Your database username
          $dbpassword = ""; // Your database password
          $connecteddb = ""; // The database name
          $dbconnect = mysql_connect ("$host","$dbuser","$dbpassword") or die (’I cannot connect to the database because: ’ . mysql_error());
          mysql_select_db ("$connecteddb");
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 30376
            • 10 Posts
            hmm...

            I’m already using a standard mysql connection like the one you show here. Instead of it being in a snippet, it’s contained in a text file within my assets>templates>"mytemplate" directory. that shouldn’t cause this type of issue, should it?
              • 4041
              • 788 Posts
              The only thing I can think of is it may not be actually including your file, you might need to append this to your file path:

              <?php
              $asset_root_path ="assets/snippets/your_snippet";
              include MODX_BASE_PATH.$asset_root_path."/your_file.php";
              ?>
                xforum
                http://frsbuilders.net (under construction) forum for evolution
                • 36702
                • 76 Posts
                I’ve handled a similar situation without worrying about making a new connection. If the modx db user has permissions in the other database, you can just use the existing connection and prefix the table name with your database name, eg
                select myfield from mydb.mytable

                You can even make a query that joins tables in both databases, eg
                select mytable.myfield, username from mydb.mytable inner join modxdb.modx_web_users on modx_web_users.id=mytable.idfield
                  • 30376
                  • 10 Posts
                  thanks, all, for your suggestions. Ultimately, what I’ve decided to do is simply move all DB content to the modx database. This is not ideal from my perspective as I like to keep databases organized and separated when it makes sense. I’m sure I’ll eventually figure out a way to access other databases from within Modx, but at this point I just need to get the sites migrated, and it’s the one method that I know will work for me 100% of the time.

                  Please chime in if you have other ideas for this--I’m subscribed to the thread and will be notified if anyone posts.

                  Thanks!