I'm continuing to struggle with xPDO connections to a foreign database in a snippet.
This works fine as code inline:
$dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'username','password');
echo $o=($xpdo->connect()) ? 'Connected<br><br>' : 'Not Connected<br><br>';
$results = $xpdo->query("SELECT * FROM `table` LIMIT 20");
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$output .= $modx->getChunk('test', $row);
}
return $output;
But when I put the connection script into a snippet 'pdoConnect'
define('MODX_CORE_PATH', '/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'username','password');
and call it using
$modx->runSnippet('pdoConnect');
it throws an error
Fatal error: Call to a member function connect() on a non-object in /home2/pghmin/public_html/core/cache/includes/elements/modsnippet/76.include.cache.php on line 22
Modx 2.2.14 and 2.5.0 same problem.
Seems trivial but I can't find a solution