We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    Hi all,

    I have a PHP file (that I cant make a snippet out of). I need to connect it to the database and rather then doing it the normal way was wondering if I could just include a header file of some sort that already has a DB connection.

    Does anyone know of such a file in the Modx file structure?

    Thanks
      • 3749
      • 24,544 Posts
      Are you sure you can't make a snippet out of it? You can make a snippet out of almost any PHP file.

      In a pinch, you can include the config.inc.php file to get the database credentials and connect to the DB directly as long as your code will run completely outside of MODX.

       $configCorePath = 'path/to/modx/root . 'config.core.php';
       include  $configCorePath;
       include MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
      
       $this->database_user = $database_user;
       $this->database_password = $database_password;
       $this->database_dsn = $database_dsn;
       $this->dbase = $dbase;
       $this->database_connection_charset = $database_connection_charset;
      
      try {
         $dbh = new PDO($this->database_dsn,
             $this->database_user, $this->database_password);
      } catch (PDOException $e) {
             echo 'connection failed') .
                 ': ' . $e->getMessage();
      }
      if ($dbh instanceof PDO) {
          $connected = 1;
      }

        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