We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hello, I'm trying to show data from another database on the same webserver. I read a lot of forum threads and I read Bob's guide on custom tables, and the documentation found here: http://rtfm.modx.com/display/xPDO20/Database+Connections+and+xPDO . But I'm not a programmer and it's all very confusing to me.

    I thought/hoped I would be able to do something like this:
    (code partly from another forum thread). This is the code from my snippet:

    $dsn = 'mysql:host=localhost;dbname=mydatabase;port=3306;charset=utf-8';
    $xpdo = new xPDO($dsn,'myuser','mypassword');
    
    echo $o=($xpdo->connect()) ? 'Connected' : 'Not Connected';
    
    $query = $xpdo->query("SELECT `name` FROM `mytable` ");
          
    if ($query) {
        $names = $query->fetchAll(PDO::FETCH_COLUMN);
       print_r($names);
    }   else { echo "nothing found"; }


    But I always get 'not connected' (and after that 'nothing found') so I don't even know if the rest of the code works. Anyone got a clue why I can't connect? I've triple checked the database name, user and password and the port is also ok.

    I'm using MODX Revolution 2.1.3-pl (traditional).

    Another question for when I manage to get connected, can I use the following code to use a chunk to output the data? Or do I really need to do the install package-thing from Bob's guide if I want that? (this guide: http://bobsguides.com/custom-db-tables.html).

    foreach($items as $item) {
        $fields = $item->toArray();
        $output .= $modx->getChunk(itemTpl', $fields);
    }
    
      • 3749
      • 24,544 Posts
      If you're sure the database name, username, password, and hostname are correct, that might not be the right port.

      Double-check the values, because some hosts add a prefix on the db name and db username, so they may not be what you entered.

      And yes, if you want to use xPDO to communicate with the DB, you'll have to create class and map files a load them, though you could use the method describe here to create the schema and run parseSchema() on it.

      http://rtfm.modx.com/display/revolution20/Using+Custom+Database+Tables+in+your+3rd+Party+Components

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 19328
        • 433 Posts
        Ok, so in theory my code to connect to the db is ok? It doesn't matter that it's another db than the modx db?

        I checked the port by running this sql:
        SHOW GLOBAL VARIABLES LIKE 'PORT';

        It says 3306.
        User and password are also really the right ones, at least, they are the same ones that are used in the modx config file.
        Strange isn't it! Do you know of a way to show more errors about the connection?

        And just to be sure, with 'use xPDO to communicate with the DB', you mean doing things like 'getChunk' and 'toArray', right?

        Thanks for your help!
          • 3749
          • 24,544 Posts
          Look in the MODX error log (on the Reports menu). Hopefully, there'll be some clues there.

          getChunk() can be used regardless, but I did mean stuff like get('field'), getObject(), getCollection(), toArray(), etc.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting