tags:

views:

37

answers:

4

how do i check if a variable is of a type mysqli object?

A: 

Take a look at get_class

AJ
+1  A: 

http://www.php.net/get_class

sidereal
+5  A: 

Try the instanceof operator, the is_a function or the get_class function:

$var instanceof MySQLi
is_a($var, 'mysqli')
is_object($var) && get_class($var) == 'mysqli'
Gumbo
weird...none of those worked...and im sure its a mysqli object cause i do a mysqli_fetch_assoc on it and it works..but when i add a if(is_a($var, 'mysqli') nothing is outputted. nor when i use the other 2..
weng
@noname: If your doing `mysqli_fetch_assoc($var)` then `$var` is not a MySQLi object but a MySQLi result resource (see http://php.net/resource). That’s something different.
Gumbo
+2  A: 

You'll probably want the instanceof operator.

It will work for derived classes as well, in the odd case that you extending or building your own wrappers.

zombat
Fine for versions PHP5+
AJ
Yes. Generally unless a poster specifies PHP4, it is safe to assume they are using PHP5 these days. PHP4 has been dead now for more than two years.
zombat