<![CDATA[ xPDO database connection script - My Forums]]> https://forums.modx.com/thread/?thread=101157 <![CDATA[xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546024
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]]>
grogorio Oct 29, 2016, 03:03 AM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546024
<![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559147 BobRay Jun 20, 2018, 03:47 AM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559147 <![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559139 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.
PlexKodiLucky Patcher]]>
nazim13 Jun 19, 2018, 05:01 PM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559139
<![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-548775 Hopefully you've hardened your installation and have you core-directory outside of your webroot.]]> Bruno17 Feb 16, 2017, 06:29 AM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-548775 <![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-548771
It was just a matter of storing the connection code in a regular php file (for example dbConnect.php) and then calling that in the snippet like so:

require "assets/connections/dbConnect.php";


Too easy]]>
grogorio Feb 15, 2017, 10:57 PM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-548771
<![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546092
I can't think of any reason why it would be different, unless you are using $modx or $xpdo as variables in your code.

OTOH, using require_once would likely be faster than runSnippet() anyway.

It would be faster yet (and would probably be more graceful in case of an error), to use "include" and check a return value from the snippet to see if the include succeeded.

$returnVal = include 'MySnippetFile';

]]>
BobRay Oct 31, 2016, 10:06 PM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546092
<![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546049
Once again it works fine if I have it all in the one snippet along with the query. But if I separate out the connection string and call it using $modx->runSnippet('pdoConnect'); I get the same error.

I'm starting to think there might be something wrong with my usage of $modx->runSnippet. My expectation was that it's functionally equivalent to a require statement, but perhaps this is not the case?

e.g. if I use require_once('assets/connections/test.php'); it works as expected so maybe that's the way to go.]]>
grogorio Oct 30, 2016, 07:50 AM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546049
<![CDATA[Re: xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546047
Since your code is using PDO, I would avoid the overhead of including the MODX class file and instantiating a second copy of xPDO. You should be able to just create a new PDO instance that connects to the foreign database and query it. $modx and $xpdo will still be available if you need them for something related to the MODX DB.

http://php.net/manual/en/pdo.construct.php

http://php.net/manual/en/pdo.query.php

]]>
BobRay Oct 30, 2016, 05:35 AM https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-546047