Page 1 of 1

User Defined Tags

Posted: Fri Aug 04, 2006 3:16 am
by argosycms
Hello,

I'm try to create a UDT that will add rows to a table I defined.  Here is the code I'm using but I'm getting some errors.  The code works fine outside CMSMS.  What did I miss?  Thanks

PHP Warning:  mysql_real_escape_string(): Can't connect to MySQL server on 'localhost' (10061) in E:\hshome\xxxxxxxxxxxxxxx\lib\adodb_lite\adodbSQL_drivers\mysql\mysql_driver.inc on line 174

$username="user";
$password="password";
$database="db";
$myserver="server";

mysql_connect($myserver,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO tblVisits(url,realip,longip) VALUES ('".$_SESSION['url']."','".$_SERVER['REMOTE_ADDR']."','".gethostbyaddr($_SERVER['REMOTE_ADDR'])."')";
mysql_query($query);

mysql_close();

Re: User Defined Tags

Posted: Fri Aug 04, 2006 3:41 am
by Dr.CSS
Is part of this the UDT you are trying to make?

If not can you paste your code?

Re: User Defined Tags

Posted: Fri Aug 04, 2006 3:46 am
by argosycms
This is the full code.  Nothing is displayed to the user, it's just for tracking.

Re: User Defined Tags

Posted: Fri Aug 04, 2006 3:51 am
by Dr.CSS
How are you putting it in the edit content box for UDT?

You may need to use the {literal}your code {/literal} tags and put it in the Source button window.

Re: User Defined Tags

Posted: Fri Aug 04, 2006 4:01 am
by Elijah Lofgren
It seems like maybe your UDT is knocking off CMSMS's DB connection.

Try this code, it will use CMSMS's DB connection:

Code: Select all

global $gCms;
$db = &$gCms->db;
// $_SESSION['url'] = 'test'; // Commented out, only used for my testing. I assume you assign a value to it elsewhere
$query = 'INSERT INTO tblVisits (url,realip,longip) VALUES (?,?,?)';
$db->Execute(
	     $query,
	     array(
		   $_SESSION['url'],
		   $_SERVER['REMOTE_ADDR'],
		   gethostbyaddr($_SERVER['REMOTE_ADDR'])
		   )
	     );

Or maybe just remove this line from your code:

Code: Select all

mysql_close();
Oh, and from the looks of the code you may be interested in my VisitorStats module: http://dev.cmsmadesimple.org/projects/visitorstats  ;)

Hope this helps,

Elijah

Re: User Defined Tags

Posted: Fri Aug 04, 2006 4:12 am
by argosycms
Thanks, removing mysql_close(); took care of the problem.  Can you explain why?

Re: User Defined Tags

Posted: Fri Aug 04, 2006 4:52 am
by Elijah Lofgren
argosycms wrote: Thanks, removing mysql_close(); took care of the problem.  Can you explain why?
I'm guessing that since CMSMS was already connected to the same database it just reused that connection automatically. So when you called mysql_close(); it closed CMSMS's connection, thus causing problems. ;)