Jump to content

[SOLVED] Return A Variable From Class Function


shane0714

Recommended Posts

I have been working on something that should be simple but I cannot figure out how to do it. Basically, I am working with a PHP class that handles MySQL queries. The code to call a query looks like looks like:

$dbcn->db_qry("DELETE FROM contactus WHERE id='$id'","1");

 

This function is inside a class called db_connect.  The function is

function db_qry($qry, $type, $from="") {
	$this->dbQryResult = mysql_query($qry) or $this->goError($qry, mysql_error(), $type, $from);
 }

 

As the function shows, if the query fails, it is directed to the goError() function where it prints an error message. The problem is that some instances, I want it to instead return a variable back out of the function. The variable holds some information so I can print out the error message later in the page. In this case, it should return a variable indicating whether the query succeeded. That variable will be used in the following part:

if ($dbcn->db_qry != false) {
	$status = '<div id="statussuccess">Your selected message(s) have been successfully deleted.</div>';
} else {
	$status = '<div id="statusfailure">There was a problem deleting the selected messages.</div>';
}

 

I'm stumped here. I've tried a number of things, but I think the closest I've gotten is to change the db_qry() function to:

function db_qry($qry, $type, $from="") {
	if($type != 1) {
		$this->dbQryResult = mysql_query($qry) or $this->goError($qry, mysql_error(), $type, $from);
	} else {
		$this->dbQryResult = mysql_query($qry);
		return $this->dbQryResult;
	}
        }

 

I know this is probably something simple, but so far I haven't figured it out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.