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