Jump to content

Warning: mysql_fetch_array() ... null given: Bad class method?


MasterACE14

Recommended Posts

Greetings,

 

I am receiving the following error...

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\system\libraries\mysql.php on line 42

 

Firstly, the query is failing when 'or die(mysql_error())' is added to it, however no error is coming up? So for whatever reason the query result is empty even though I've checked there is atleast one record in the table, and the table name is correct.

 

$q = $db->query("SELECT * FROM `cg_accounts`"); /*or die("bugger, an error: ".mysql_error());   <<< if this isn't commented out I get no error at all, however with it commented out I get the aforementioned error */
while($row = $db->fetch_array($q)) {
    echo $row['username'];
}

 

I can't see anything wrong with my MySQL class methods either...

public function query($sql) {
		$this->last_query = $sql;
                        $result = mysql_query($sql);
		$result = self::confirm_query($result);
		return $result;
	}

	public function fetch_array($result_set, $result_type='MYSQL_BOTH') {
		return mysql_fetch_array($result_set, $result_type); // line 42
	}

	private function confirm_query($result) {
		if(!$result) {
			trigger_error('Database query confirm failed: ' . mysql_error(), E_USER_ERROR);
		}
	}

 

Cheers,

Ace

Your need to return $result in your confirm_query() function

		private function confirm_query($result) {
		if(!$result) {
			trigger_error('Database query confirm failed: ' . mysql_error(), E_USER_ERROR);
			return false;
		}
		return $result;
	}

that did the trick, Thanks.

 

But I'm lost... I've used this same MySQL class on sites before, except most of the methods were static and it has always worked? Any ideas as to why it doesn't want to work now without those changes you mentioned? (always been on the same PHP/MySQL settings and versions locally as well)

Without those changes the $result variable will be set to null as your static function is not returning anything.

$result = self::confirm_query($result);

Which is overriding the value returned by mysql_query() earlier on. mysql_fetch_assoc expects a results resource to be given to it. That is what should be returned when you call $db->query().

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.