MasterACE14 Posted July 25, 2011 Share Posted July 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242741-warning-mysql_fetch_array-null-given-bad-class-method/ Share on other sites More sharing options...
wildteen88 Posted July 25, 2011 Share Posted July 25, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/242741-warning-mysql_fetch_array-null-given-bad-class-method/#findComment-1246747 Share on other sites More sharing options...
MasterACE14 Posted July 25, 2011 Author Share Posted July 25, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/242741-warning-mysql_fetch_array-null-given-bad-class-method/#findComment-1246753 Share on other sites More sharing options...
wildteen88 Posted July 25, 2011 Share Posted July 25, 2011 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(). Quote Link to comment https://forums.phpfreaks.com/topic/242741-warning-mysql_fetch_array-null-given-bad-class-method/#findComment-1246756 Share on other sites More sharing options...
MasterACE14 Posted July 25, 2011 Author Share Posted July 25, 2011 ah ok, thanks for the info! Quote Link to comment https://forums.phpfreaks.com/topic/242741-warning-mysql_fetch_array-null-given-bad-class-method/#findComment-1246758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.