Need to write many snippets using direct DB access. And got simple a question, I can’t find an answer to:
what do I need to include in my snippet to use default MODx DB connection settings so I don’t need to write $modx->db->connect every time.
-
☆ A M B ☆
- 24,524 Posts
If it’s the same database that MODx is using, you don’t have to connect at all, MODx is already connected when the snippet is processed.
Great.
But what if the DB is different, but the server, username and password are the same?
-
☆ A M B ☆
- 24,524 Posts
You have to explicitly connect to it, with the host and login information. It might be worth trying something like this:
$modx->db->connect('localhost','dbname');
The default parameters should work for the rest of it.
See
http://wiki.modxcms.com/index.php/API:DBAPI:connect