We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7455
    • 2,204 Posts
    would it be posible to grab conntent from another database on the same server to use in mod-x?

    My idea is this:
    I have multiple dealer sites that are able to edit there own content, but there is a part that is only editable for the main office, things like latest news and advertisements.

    I have one portal that is only editable by the maine office and the newsitems from that page must also be placed on the dealer sites.
    Now I could copy the news over to all the dealer sites but if I would have a snippet that could access another database (on the same server and with rights to do so) and get the newsitems into the dealer page, would save a lot of time.

    I think that it is possible with our API but I do not know how to acoplisch this. could somone help me start?

    Thanks alot

    Dimmy
      follow me on twitter: @dimmy01
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Yes, it can be done. You need to have the login and database information for the reomote database, and your snippet can check the user’s SESSION for permissions before doing anything; with the right permissions it will connect to the remote database and get whatever you want.

      The parser class file even has a couple of functions for dealing with remote databases;

      dbExtConnect($host, $user, $pass, $dbase) 
      
      getExtTableRows($host="", $user="", $pass="", $dbase="", $fields="*", $from="", $where="", $sort="", $dir="ASC", $limit="")
      
      putExtTableRow($host="", $user="", $pass="", $dbase="", $fields="", $into="")
      
      updExtTableRow($host="", $user="", $pass="", $dbase="", $fields="", $into="", $where="", $sort="", $dir="ASC", $limit="")
      


      So basically all you need to do is check the user’s SESSION for permission, then do
      $stuff_i_want = $modx->getExtTableRows(<host-name-or-address>, <remote username>, <remote password>, <remote database name>, <fields to get>, <table name>, <data for where>, <field to sort by>, <sort direction>, <number of rows to get>);


      This returns an array all ready to use.
        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
        • 7455
        • 2,204 Posts
        Nice i think I get it will try making a snippet to get some content from anothet database,
        I will keep you posted and I hope that if I need some more help that I can ask it here.

        Thanks again

        Dimmy
          follow me on twitter: @dimmy01
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          It it’s on the same server, the $host is most likely "localhost". Watch out for the table prefix; if you left it as default when you installed, it will be modx_, as in modx_site_content.
            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
            • 7455
            • 2,204 Posts
            is it posible to make that prefix as a varible that way i do not need to add it to every database call i do
              follow me on twitter: @dimmy01
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Sorry, missed your last post!

              You can find out the $table_prefix varible as well as the $dbase database name from the relevant manager/includes/config.inc.php file, and use it like this:
              $remote-dbase = "otherDatabaseName";
              $remote-table_prefix = "modx_";
              $sql = "SELECT * FROM `" . $remote-dbase . "`." . $remote-table_prefix ."table_name" WHERE ...


              to get

              `otherDatabaseName`.modx_table_name


              For your local tables, you can use
              "SELECT * FROM " . $modx->getFullTableName('table_name') . " WHERE ...

                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
                • 7455
                • 2,204 Posts
                Thanks Susan
                  follow me on twitter: @dimmy01
                  • 17270
                  • 13 Posts
                  I’ve been trying to use the getExtTableRows function. I get the following error message:

                  MODx encountered the following error while attempting to parse the requested resource:
                  « Execution of a query to the database failed - »
                        SQL: SELECT industry_id, name FROM er.industries ORDER BY name ASC ; 


                  If I copy+paste the generated SQL query directly into the DB WebAdmin, it prints out the result nicely, so the query itself is fine. Here’s how I use the function:

                  $result = $modx->getExtTableRows("localhost", "<username>", "<password>", "<db_name>", "industry_id, name", "industries", "", "name", "ASC", "");
                  while ($row = $modx->db->getRow($result))...


                  Any help is very much appreciated.
                  Algirdas
                    • 27376
                    • 576 Posts
                    Quote from: livewire at Jul 20, 2007, 12:50 PM

                    I’ve been trying to use the getExtTableRows function. I get the following error message:

                    MODx encountered the following error while attempting to parse the requested resource:
                    « Execution of a query to the database failed - »
                          SQL: SELECT industry_id, name FROM er.industries ORDER BY name ASC ; 

                    Try enclosing the db_name and table in backticks "`" like this:
                    <?php
                    $result = $modx->getExtTableRows("localhost", "<username>", "<password>", "`<db_name>`", "industry_id, name", "`industries`", "", "name", "ASC", "");
                      • 17270
                      • 13 Posts
                      Thanks sirlancelot, the backticks seem to work. But MODx gives another error:

                      « Execution of a query to the database failed - Access denied for user ’er_usr’@’%’ to database ’`er`’ »

                      Any idea where the "%" instead of "localhost" comes from? I checked the code - no % signs anywhere...

                      Thanks again,
                      Algirdas