How to us CMSMS db connection

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.
Locked
Glenn

How to us CMSMS db connection

Post by Glenn »

I know Wishy has said you can use the exisiting connection, but I can't figure it out. I know I'm compromising my security, but my solution below is all I can figure out. Any help is appreciated.

When i need to do a custom query to the database this is what I do. I've created a plugin in the plugins folder called function.dblink.php that looks like this:

Code: Select all

function smarty_cms_function_dblink($params, &$smarty)
{
global $gCms;

$vars = &$gCms->variables;

//Setup the database connection
	$dbhost         = "XXX";
	$dbusername     = "XXX";
	$dbuserpass     = "XXX";
	$default_dbname = "XXX";
	$dblink = mysql_connect($dbhost, $dbusername, $dbuserpass);
	
	if (!$dblink)
		{
 		  die('Could not connect: ' . mysql_error());
		}
	else
		{
			mysql_select_db($default_dbname);
	       $vars['dblink'] = $dblink;
		}	
}
Then I create a user-defined tag when needed that start like this:

Code: Select all

global $gCms;
$dblink = $gCms->variables['dblink'];

$query = "SELECT * FROM cms_content WHERE metatdata LIKE '%beef%'";
$result = mysql_query($query,$dblink);

and so on...

What's the best way to do this?
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm

Re: How to us CMSMS db connection

Post by Ted »

Use this for your used defined tag

Code: Select all

global $gCms;
$db =& $gCms->GetDb();

$query = 'SELECT * FROM cms_content WHERE metadata LIKE '%beef%';
$result = &$db->Execute($query);

while ($result && !$result->EOF)
{
	$id = $result->fields['content_id'];
	$title = $result->fields['content_name'];
	$result->MoveNext();
}
etc...
Locked

Return to “CMSMS Core”