# Solved: PHP Class Method - set optional method parameter



## MRdNk (Apr 7, 2007)

Hi All,

How can I set an option parameter for a class method?
Basically I have a method that queries the database, currently its setup to receive an sql statement, via "function queryDB($sql){...} and returns to the class scope variable $resultset, which another method uses to verify the username and password.

However, I want to set an optional parameter for the type of database query so that I can have an if statement to say whether the type of query is a query (therefore requiring a resultset back) or an update (not needing/wanting the resultset to be returned), and then additional types when I've got this working.

There is probably a better way around this, please let me know.

I think I've explained well enough, what I'm looking for, however, I'm happy to post my class if it helps.

I've tried & but it says not enough arguments provided when I don't refer to them both.

```
function queryDB($sql, &$type){
...
if($type="update")	{
//update option
...
}
else {
// non-update option
}

...

function verifyUser($username, $password){
...
$this->queryDB($sql);
...
}
...

function changePassword($newpassword){
...
$this->queryDB($sql,"update");
}
```


----------



## MMJ (Oct 15, 2006)

```
function queryDB($sql = '', &$type){
```


----------



## MRdNk (Apr 7, 2007)

Is that making $sql the optional parameter, or $type? 
Its $type that I want to be optional.

The following is now happening:

```
function verifyUser(...){
$this->queryDB($sql);
...
}
```
Is giving me the following error:
Fatal error: Call to a member function fetchRow() on a non-object in E:\Apache2.2\htdocs\MRdnK\objects.php on line 56

Line 56 is:

```
while($row = $resultset->fetchRow(MDB2_FETCHMODE_ASSOC)) {
...
}
```


----------



## MMJ (Oct 15, 2006)

Corrected


```
function queryDB($sql, &$type = ''){
```
---

This error occurs after you change the code or what?


----------



## MRdNk (Apr 7, 2007)

This is the error I'm getting when trying to extend with the optional parameters. 

If I just use the $sql then it works fine, but as soon as I try to extend it like above, it gives me this error.


----------



## MRdNk (Apr 7, 2007)

The complete class is:


```
class userLogin	{
	var $username="";
	var $password="";
	var $userId=0;
	var $loggedIn = false;
	var $results="";

	function queryDB($sql, &$type=''){ 
		require_once('MDB2.php');
		require('db_login.php'); 
		$url = "mysql://$db_username:[email protected]$db_host/$db_database";
		$con = MDB2::factory($url);
		if(PEAR::isError($con)) {
    		die("Error while connecting : " . $con->getMessage());
		}
		else {
			if($type='update')	{
			}
			elseif($type=='')	{
				$resultset = $con->query($sql);
				if(PEAR::isError($resultset)) {
					die('Failed to issue query, error message : ' . $resultset->getMessage());
				}
				else	{
					$this->results=$resultset;
				}
			}
		}
	}

	function verifyUser($username, $password)	{
		$this->username = $username;
		$this->password = $password;
		$sql = "SELECT * FROM users WHERE user_name = '$username' AND password = 'md5($password)'";
		$this->queryDB($sql);
		$resultset=$this->results;
		while($row = $resultset->fetchRow(MDB2_FETCHMODE_ASSOC)) {
			foreach($row as $field => $value) {
				if($field=='user_id') {
					$userId = $this->value;	
					$this->loggedIn=true;
				}
			}
		}	
		$this->results="";
		$username = $this->username;
		$loggedIn = $this->loggedIn;

	}

	function showUsername()	{
		if($this->loggedIn=true)	{
			$username=$this->username;
			$results=$this->results;
			echo "Welcome $username $results";
		}
	}

	function changePassword($newpassword)	{
		if($this->loggedIn=true)	{
			$user_id=$this->user_id;
			$sql="UPDATE users SET password = 'md5($newpassword)' WHERE user_id=$user_id";
			$this->queryDB($sql,'update');

		}
	}

}
?>
```
P.s. Any critiques of this class appreciated.


----------



## MRdNk (Apr 7, 2007)

Right, well I've gone with this solution for now, however would still appreciate some feedback on why the previous method doesn't work:


```
class userLogin	{
	var $username="";
	var $password="";
	var $userId=0;
	var $loggedIn = false;
	var $con=NULL;

	function queryDB($sql){ 
		require_once('MDB2.php');
		require('db_login.php'); 
		$url = "mysql://$db_username:[email protected]$db_host/$db_database";
		$con = MDB2::factory($url);
		if(PEAR::isError($con)) {
    		die("Error while connecting : " . $con->getMessage());
		}
		else	{
			$this->con=$con;
		}
	}

	function verifyUser($username, $password)	{
		$this->username = $username;
		$this->password = $password;
		$sql = "SELECT * FROM users WHERE user_name = '$username' AND password = 'md5($password)'";
		$this->queryDB($sql);
		$con=$this->con;
		$resultset = $con->query($sql);
		if(PEAR::isError($resultset)) {
			die('Failed to issue query, error message : ' . $resultset->getMessage());
		}
		else	{
			while($row = $resultset->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				foreach($row as $field => $value) {
					if($field=='user_id') {
						$userId = $this->value;	
						$this->loggedIn=true;
					}
				}
			}	
		}
		$this->results="";
		$username = $this->username;
		$loggedIn = $this->loggedIn;

	}

	function showUsername()	{
		if($this->loggedIn=true)	{
			$username=$this->username;
			echo "Welcome $username";
		}
	}

	function changePassword($newpassword)	{
		if($this->loggedIn=true)	{
			$this->sql='';
			$user_id=$this->user_id;
			$sql="UPDATE users SET password = 'md5($newpassword)' WHERE user_id=$user_id";
			$this->queryDB($sql);

		}
	}

}
```


----------

