Page 1 of 1

Re: Multiple database case study - additional info?

Posted: Fri May 28, 2010 2:14 pm
by calguy1000

Code: Select all

global $gCms;
$db = $gCms->GetDb();
$query = 'SELECT * FROM action';
$dbr = $db->Execute($query);

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

Posted: Thu Sep 23, 2010 5:53 pm
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 ;)

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

Posted: Sat Oct 15, 2011 1:43 am
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();