Jump to content

Need help preforming a mysql_fetch_array with OOP


eldan88

Recommended Posts

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.