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
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;
}