Page 1 of 1

Database error checking?

Posted: Sun May 28, 2006 8:57 pm
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.

Re: Database error checking?

Posted: Sun May 28, 2006 9:29 pm
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.

Re: Database error checking?

Posted: Mon May 29, 2006 1:33 am
by boscopup
Hmmm... just did that, and I didn't see my query at all. :( I'm going to tinker some more...

Re: Database error checking?

Posted: Mon May 29, 2006 4:24 am
by Ted
Do you have a $db object?

Code: Select all

$db =& $this->GetDb();

Re: Database error checking?

Posted: Mon May 29, 2006 4:31 am
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.

Re: Database error checking?

Posted: Mon May 29, 2006 5:11 am
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.

Re: Database error checking?

Posted: Mon May 29, 2006 8:56 pm
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.

Re: Database error checking?

Posted: Mon May 29, 2006 11:40 pm
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!!!

Re: Database error checking?

Posted: Tue May 30, 2006 12:37 am
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 :)