User Defined Tags

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
argosycms

User Defined Tags

Post 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();
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: User Defined Tags

Post by Dr.CSS »

Is part of this the UDT you are trying to make?

If not can you paste your code?
argosycms

Re: User Defined Tags

Post by argosycms »

This is the full code.  Nothing is displayed to the user, it's just for tracking.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: User Defined Tags

Post 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.
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: User Defined Tags

Post 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
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
argosycms

Re: User Defined Tags

Post by argosycms »

Thanks, removing mysql_close(); took care of the problem.  Can you explain why?
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: User Defined Tags

Post 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. ;)
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
Post Reply

Return to “Developers Discussion”