Hello, I am using a database class that I wrote for my project. I created a query variable that is accessible to the whole class, so that when you query the database, the class stores the query so you can use the other functions without telling it the query to use. My problem is that in a few of my functions, the query is showing up as a NULL value, but it does not in others.

here is a piece of my class:

protected $mysqlQuery;
	
	## Query DB ##
	public function query($query){
		$this->mysqlQuery = mysql_query($query);
		$rowNum = 0;
		$fields = $this->getFieldNames();
		while($row = $this->fetchArray()){
			for($i = 0; $i < count($fields); $i++){
				$results[$rowNum][$fields[$i]] = $row[$fields[$i]];
			}
			$rowNum++;
		}
		var_dump($this->mysqlQuery); // This returns the query properly
		return $results;
	}
	
	## Fetch results of query ##
	protected function fetchArray($query = ""){
		var_dump($this->mysqlQuery); // This returns the query properly
		if($query == ""){
			return mysql_fetch_array($this->mysqlQuery);
		}elseif($query != ""){
			return mysql_fetch_array($query);
		}else{
			return false;
		}
	}
	
	## Get number of rows in query ##
	public function numRows($query = ""){
		var_dump($this->mysqlQuery); // This returns the query as a NULL value
		if($query == ""){
			return mysql_num_rows($this->mysqlQuery);
		}elseif($query != ""){
			return mysql_num_rows($query);
		}else{
			return false;
		}
	}

I have tried changing the scope of the variable to public, and private and neither of those help to change the error. I have also had problems in the past with accidentally overriding variables to null values (like in an if() statement to see if the value is equal to null, I set it to null with a = instead of ==), but I am reasonably sure that is not the problem here because I have looked specifically for that.

Thank you for any help that you can give,
Key Roche'

Are you running the numRows() function before running query()? That would cause that problem.

Are you running the numRows() function before running query()? That would cause that problem.

No, I am running the query first, but I think the problem may be with another one of my classes that uses the database class because it works fine is I am just running a query and then number of rows without involving another class.
I have an accounts class that is used for accessing and modifying our accounts for clients. It has a function that gets the information for all of the accounts so that I can make a table of them. The code for it is:

function getInfo($columns = "", $where = "", $limit = ""){
		if($columns != ""){ // Columns is an array of columns to fetch each separated by :
			$columns = explode(":", $columns);
		}else{
			$columns[0] = "*";
		}
		
		$sql = "SELECT ";
		for($i = 0; $i < count($columns); $i++){
			$sql .= $columns[$i];
			if($i < count($columns) - 1){
				$sql .= ", ";
			}
		}
		$sql .= " FROM accounts";
		if($where != ""){
			$sql .= " WHERE $where";
		}
		
		if($limit != ""){
			$sql .= " LIMIT $limit";
		}
		
		$query = $this->db->query($sql);
		if($query){
			return $query;
		}else{
			return "Failure";
		}
	}

The accounts class has a variable defined for the whole class that is database, that is why there is $this->db-> . The page that I use this account class on, does not call another query except for through this function. Could the problem be that it does not save the query for some reason if it is called through this class?

I have tried to work on it and see what is wrong. It seems like the function numRows() itself knows that the variable $mysqlQuery is a query resource, but as soon as it is going into the mysql_num_rows() it looks NULL.

function numRows(){
		echo "From numRows(): "; var_dump($this->mysqlQuery);
		$numberOfRows = mysql_num_rows($this->mysqlQuery);
		echo $numberOfRows;
		return $numberOfRows;
	}

That code is just for me to debug. What I did was I have it dumping the variable $this->mysqlQuery , and it shows as this: resource(8) of type (mysql result) , which is correct.Then it goes on to get the number of rows, and then it echos it and echos the correct value, but I think it still returns a NULL because I still get the error: "Warning: mysql_num_rows() expects parameter 1 to be resource, null given in includes/database.inc.php on line 57". Also, the code that I have checking to see if the number of rows is greater than zero is still showing as false.

So now I am really confused... :\

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.