Page 1 of 1

How to us CMSMS db connection

Posted: Fri Jun 23, 2006 1:41 am
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?

Re: How to us CMSMS db connection

Posted: Fri Jun 23, 2006 1:46 am
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...