Passing mysqli result to a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nqaul
Forum Newbie
Posts: 1
Joined: Sat Jan 22, 2005 5:42 am

Passing mysqli result to a class

Post by nqaul »

Hi,

I need to pass the results of a query to a seperate class
but I keep getting an error saying that a mysqli object is
required.

Ex:

With a class like:

Code: Select all

class Test
{
  function Do_Stuff($results)
  {
    while ($row = mysqli_fetch_array($results)
      echo $rowї0];
  }
}
Assuming I've done the db con correctly and included
everything I need in a script like:

Code: Select all

$obj = new Test();
$res = mysqli_query($dbcon, $query);

obj->Do_Stuff($res);
When $res is passed to the Do_Stuff function I get the error:
"mysqli_fetch_array() expects parameter 1 to be mysqli, object
given in ....filename...."

but when I use the $res in the script its fine.

Can you not pass query result objects or is this loosing scope?

Thanks in advance
nqaul
Post Reply