eldan88 Posted February 5, 2013 Share Posted February 5, 2013 Hi, I tried to prefrom a mysql_fetch_array using OOP and i get the folloing error message. Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in .... filename I have created a class named mysql database, and inside that class I have included a method named "fetch_array". Below is how the code looks. (There are obviously other mehtods in that class i have just included the one I am having trouble with) <?php // I have created a file named database.php and created the class mySQLDatabase for that file class mySQLDatabase { public function fetch_array($result_set) { return mysql_fetch_array($result_set); } public function query($sql) { $result = mysql_query($sql, $this->connection); $this->confirm_query($result); // end of if (!$result) return $result; } // end of function query($sql) } $database = new mySQLDatabase();//Created an instance of the class to include in my user.php file. (Below) ?> then i have created a new file named user.php.. included the database.php and have created a class the preforms the queries called the "User" Class <?php class User { public static function find_all() { $result_set = self::find_by_sql("SELECT * FROM users"); return $result_set; } public static function find_by_id($id=0) { // I have pulled in the global $database variable that I have instantiated from the database.php file global $database; $result_set = self::find_by_sql("SELECT * FROM users WHERE id={$id} LIMIT 1"); // I used the find_by_sql(below) method to perform my functions for functions inside this class $found = $database->fetch_array($result_set); return $found; } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $return = $result_set; } } ?> Lastly on my index.php i used the static method to run the query and preform the mysql_fetch_array on the username and it doesn't seem to be working. here is how did this on the index.php file. $record = User::find_by_id(1); echo $record['username']; Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/ Share on other sites More sharing options...
requinix Posted February 5, 2013 Share Posted February 5, 2013 find_by_sql() isn't returning anything: $return = $result_set; Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/#findComment-1410139 Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 You need to check for SQL errors after running the query. See the link in my signature. Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/#findComment-1410140 Share on other sites More sharing options...
eldan88 Posted February 5, 2013 Author Share Posted February 5, 2013 oh wow i see that. I put a dollar sign next to return accidently! Thank you requnix! find_by_sql() isn't returning anything: $return = $result_set; Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/#findComment-1410141 Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 You need to remove the = too. Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/#findComment-1410143 Share on other sites More sharing options...
eldan88 Posted February 5, 2013 Author Share Posted February 5, 2013 Thanks! I'll look into that now You need to check for SQL errors after running the query. See the link in my signature. Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/#findComment-1410147 Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 After rereading your post its possible your confirm_query function does that, requinix found the problem in your posted code. But ensure your code does capture SQL errorsz Quote Link to comment https://forums.phpfreaks.com/topic/274037-need-help-preforming-a-mysql_fetch_array-with-oop/#findComment-1410148 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.