We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45617
    • 24 Posts
    Hello everyone,

    have a quick look, please, what I'm doing wrong. The snippet shows below my text works perfect (with result - Connectedadminpeterjason), but it shouldn't be, because in database "adminer" there is no table "modx_users"!
    This such a table is in my main database, so the login information in header of snippet are ignored- why?:-)
    (database Adminer was found for the simple filling via Adminer - http://www.adminer.org/en/)

    Thank all of you for spending time.
    Peter

    // Criteria for foreign Database
     $host = 'localhost';
     $username = 'adminer';
     $password = 'password';
     $dbname = 'adminer';
     $port = 3306;
     $charset = 'utf-8';
    
     $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
     $xpdo = new xPDO($dsn, $username, $password);
    
     // Test your connection
     echo $o = ($xpdo->connect()) ? 'Connected' : 'Not Connected';
    
     // Issue queries against the foreign database:
     $stmt = $modx->query('SELECT * FROM `modx_users` ORDER BY `username` ASC');
     if ($stmt && $stmt instanceof PDOStatement) {
     while ($row= $stmt->fetch(PDO::FETCH_ASSOC)) {
     echo $row['username'];
     }
     }
      • 22303 MODX Staff
      • 10,725 Posts
      You are creating an $xpdo object for the external database, but you are using the $modx for the queries. Those are two separate PDO instances, and two separate connections.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Indeed, I was thinking that it might be a better idea to use a unique name for the connection, rather than $xpdo, just to avoid any conflicts or confusion.
          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
          • 45617
          • 24 Posts
          I've got this, I have to change $modx->query('SELECT * FROM `tables_taxoms` ORDER BY `name` LIMIT 50'); to $xpdo->query('SELECT * FROM `tables_taxoms` ORDER BY `name` LIMIT 50'); ...

          sometimes I'm a little bit slower:-)

          THANK YOU !! [ed. note: petrunkov last edited this post 12 years, 6 months ago.]