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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.