[solved] Multiple database case study - additional info?

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Multiple database case study - additional info?

Post by calguy1000 »

Code: Select all

global $gCms;
$db = $gCms->GetDb();
$query = 'SELECT * FROM action';
$dbr = $db->Execute($query);
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
NaN

Re: [solved] Multiple database case study - additional info?

Post by NaN »

aimerlamer wrote:
That simply connects you to the CMS database.  
Well, this is what this function is actually meant for.
C7Mike wrote:
why the variable $this has issues
The variable does not have any issue.
You just use it in the wrong context.
The tutorial you found seems to be meant to be used in modules only not in plugins or UDTs. In plugins or UDTs you need to use $gCms instead of $this.

Since the tutorial you used includes modifications to the core files of CMSms do not expect any further support here. The dev team cannot provide help to changes to core files to use them in a way they are not created for. Hope you get my meaning.

Sorry to tell you that but that's the way it is:

http://forum.cmsmadesimple.org/index.ph ... 132.0.html

Maybe in later releases there will be a function included in the module api or wherever in the core that is explicitly created for the purpose to connect to an external database. And if so, you can count on support for it ;)
Last edited by NaN on Thu Sep 23, 2010 5:55 pm, edited 1 time in total.
mattearle
New Member
New Member
Posts: 2
Joined: Sat Oct 15, 2011 1:36 am

Re: [solved] Multiple database case study - additional info?

Post by mattearle »

I figured out how to do this by looking at how Drupal and Joomla do it. Incidentally, this is to post Wordpress posts on CMS Made Simple from a separate database on the same server.

You just have to make a connection to the external database in the UDT and then at the end, reconnect to the CMS Made Simple database. My code looks like this:


Code: Select all


$wpdbhost = 'localhost';
$wpdbuser = 'BLA_wp';
$wpdbpass = 'BLA';
$wpdbname = 'BLA_wp';

	
mysql_connect($wpdbhost, $wpdbuser, $wpdbpass) or die ('Error connecting to mysql');
mysql_select_db($wpdbname);

$vars = $gCms->variables;

$query2 = "SELECT BLA BLA_date DESC LIMIT 8";
$result2 = mysql_query($query2) or die("Error: " . mysql_error());
if( !$result2 )
{
    echo 'DB error: '. $db2->ErrorMsg()."<br/>";
}

echo print_r($row, true);
echo '<ul>';
while($row = mysql_fetch_array( $result2 )) {
   // Print out the contents of each row into a table
$date_format = $row["date_format"];
$post_title = $row["post_title"];
$post_name = $row["post_name"];
$post_excerpt = $row["post_excerpt"];
$post_content = $row["post_content"];
$postshort = myTruncate($post_content, 50);
   echo "<li><a href='/blog/$date_format/$post_name/'>$post_title</a><br>";
echo "$postshort</li>";}
echo '</ul>';

//RECONNECT TO CMS MADE SIMPLE DB
$DIR = "/home/USERNAME/public_html";

// CMS database credentials are stored in db.class.php
require_once("$DIR/db.class.php");
// the key part is below
$db = new db;
$db->_open();

Post Reply

Return to “CMSMS Core”