Need to add some PHP to talk to a mySQL DB
Permalink
I'm editing an existing Concrete 5 site, and have a mySQL DB setup on the hosting platform, but now need to figure out how to add the necessary PHP code to the Concrete pages to connect to the DB and perform the functions I need it to. I can't seem to find a way.. I'd manually insert it into the DB held page content if I could find it, but would rather believe there's a cleaner way to do this. Help???
//Load external database $db = Loader::db('Server', 'Username', 'Password', 'Database Name', true); $sql = 'SELECT * FROM sometable WHERE somefield = ? AND someotherfield = ?'; $vals = array('abc', 'xyz'); $result = $db->query($sql, $vals); $rows = $result->GetArray(); foreach ($rows as $row) { echo $row; } //Reset db back to concrete5 to avoid problems $db = Loader::db(null, null, null, null, true);
THx.. but I'm unsure how and where this code would go in a templated page???
Are you talking about just connecting your Concrete5 site to the MySQL database for its usual operation, or do you have an additional database that powers custom functionality outside the CMS?
I need to connect to a completely different database to perform other functions..
Okay, well you have two options: you can try to use the "Simple PHP" block (http://www.concrete5.org/marketplace/addons/simple-php-block/... ), or you can create a single_page -- add a new file in your site's "single_pages" directory, put whatever code you need in there, then go to Dashboard -> Pages and Themes -> Single Pages, and scroll down to the bottom. In the "Add New Page" form, type in the name of that new file you made but WITHOUT .php at the end (for example, if your code file was called "customer_lookup.php", then you'd put in "customer_lookup" without the quotes). Now if you go tohttp://yoursite.com/customer_lookup... (or whatever the name of that file is you used), you should see all of the code from that file outputted within the "skin" of your site.
Alright, I'll give that a try.. not to be a total noob.. but could you clarify "put your code in there"... in where? Once the page is created, how do I get behind the scenes to add the code?
this sounds like a single page type problem to me.
http://www.concrete5.org/documentation/developers/pages/single-page...
http://www.concrete5.org/documentation/developers/pages/single-page...
message edited/removed, figured it out
What's the HTML editor?
Hey Jordan - thanks for this snippet. I'm trying to display some records from a table in my concrete5 database in a unique way and since the server, username, etc is all the same as the global variables in my config.php is there a way to reference that information? Something like:
Thanks for your help.
$db = Loader::db('DB_SERVER', 'DB_USERNAME', 'DB_PASSWORD', 'DB_DATABASE', true);
Thanks for your help.
Nevermind, figured out it was as simple as doing:
$db = Loader::db();
Everything in concrete5 is powered from the DB. You might want to
check out single pages as a model for doing a direct DB lookup.
best wishes
Franz Maruna
CEO - concrete5.org
http://about.me/frz