Database error checking?

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
boscopup
Forum Members
Forum Members
Posts: 35
Joined: Wed May 04, 2005 3:48 pm

Database error checking?

Post by boscopup »

I'm working on my first module, and I have a simple form in the admin panel to take 3 values. I am trying to insert these values into the database table that was setup on install of the module. The problem is... the values aren't inserting, and I don't know where to go for an error message? I'm trying to use $db->ErrorMsg(), but it's not returning anything. Help? :) Some code follows...

Code: Select all

// Add contact to db
$query = "INSERT INTO " .cms_db_prefix()."module_hotccontacts_contacts".
	      " (name,email,position) VALUES (?,?,?)";
$p = array('name','email','position');
$dbresult=$db->Execute($query, $p);
if (!$dbresult) {
  	$params2['error'] = $db->ErrorMsg();
  	$this->displayadminpanel($id,$params2,$returnid);
}else {
        $this->Redirect($id,'defaultadmin',$returnid);
}				
In the displayadminpanel function, I have it look for the error param and display it in the admin panel. If I remove $db->ErrorMsg() and replace it with something else, that something else does show up. So apparently, $db->ErrorMsg() isn't returning anything, but there is obviously an error of some sort since my fields didn't insert into the database.
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Database error checking?

Post by Elijah Lofgren »

Try enabling:

Code: Select all

$config['debug'] =true;
in config.php

Then find your query in the list of queries at the bottom of the page.
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. :)
boscopup
Forum Members
Forum Members
Posts: 35
Joined: Wed May 04, 2005 3:48 pm

Re: Database error checking?

Post by boscopup »

Hmmm... just did that, and I didn't see my query at all. :( I'm going to tinker some more...
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Database error checking?

Post by Ted »

Do you have a $db object?

Code: Select all

$db =& $this->GetDb();
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Database error checking?

Post by Elijah Lofgren »

Ted wrote: Do you have a $db object?

Code: Select all

$db =& $this->GetDb();
I use:

Code: Select all

$db = $this->cms->db;
I'm guessing both will work, it's probably better to use the $db =& $this->GetDb(); method.
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. :)
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Database error checking?

Post by Ted »

There could be an issue with the double -> syntax w/ php4.  Sometimes it works, sometimes not.  I haven't quite nailed down why.
boscopup
Forum Members
Forum Members
Posts: 35
Joined: Wed May 04, 2005 3:48 pm

Re: Database error checking?

Post by boscopup »

I've got

Code: Select all

$db = &$this->cms->db;
at the top of my function. I am using php4, but I copied this code from other modules I'm using, and it's working fine in those modules, so theoretically, it should be working. Plus I use it in this module in the Install function. Hmmm...

Are there debug functions I can stick somewhere to print out stuff when debug is turned on? Maybe that would help me work through this.
boscopup
Forum Members
Forum Members
Posts: 35
Joined: Wed May 04, 2005 3:48 pm

Re: Database error checking?

Post by boscopup »

Ok, I figured it out... Stupid me had this stuff within an if statement checking if a particular param was set. Well... I had the param name wrong.  ::)

I thought I'd had data going from within that if statement, but I guess not. Must have changed it to outside and then moved it back in. Ugh. Stupid stupid stupid. :) But my insert is now working, and I should be able to move forward with this module, finally! :)

Btw, I must say, with the exception of my own stupid mistakes (mostly because I haven't been doing alot of programming in the last several months - way too rusty), the making of a CMSMS module seems to be pretty straightforward, with the help of ModuleMaker! So thanks so much for a great product!!!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Database error checking?

Post by calguy1000 »

Remember, program in baby steps, baby steps I tell ya..... and if you can't do that, well then debug in babysteps.

I add tonnes of debug statements in when I'm trying figure this stuff out and it almost always comes down to me being stupid.

and I have now almost developed a religion about how I program because of all of the mistakes I have learned from :)

Keep plugging away :)
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Post Reply

Return to “Modules/Add-Ons”