PHP Classes

$db unknown variable in included fonction

Recommend this page to a friend!

      Ultimate MySQL  >  All threads  >  $db unknown variable in included...  >  (Un) Subscribe thread alerts  
Subject:$db unknown variable in included...
Summary:mysql.class not available in included file
Messages:3
Author:Claude Gélinas
Date:2013-12-28 20:05:42
Update:2013-12-30 03:15:52
 

  1. $db unknown variable in included...   Reply   Report abuse  
Picture of Claude Gélinas Claude Gélinas - 2013-12-28 20:05:42
If I try to do the following operation in an included fonction: (include "window.inc" which contain the function I want to call)
$res = $db->QueryArray($quoi,MYSQLI_ASSOC);
I get the following error message:
Notice: Undefined variable: db....
In the main php file I can do all operation on the database but when I call the included fonction There is no connection to the mysql class

How do I proceed to have the mysql class available in the included file/function

Using PHP 5.5.7 version

  2. Re: $db unknown variable in included...   Reply   Report abuse  
Picture of Claude Gélinas Claude Gélinas - 2013-12-30 03:15:52 - In reply to message 1 from Claude Gélinas
Update, This is what I want to do and what I get:
in my php script:
$db = new MySQL();
...
$result = myfunc($a,$b,$db)

function myfunc($a,$b,$obj){
var_dump ($obj); // to see if $db is passed corectly
...
$sql = "select......";
$res = $obj->QueryArray($sql,MYSQLI_ASSOC);
...
return something;
}

var_dump is dumping the $obj correctly. It's the same if I dump it inside the myfunc or directly in the php script.

but I dont have access to the QueryArray method in myfunc
$res = false always

Ihave access to QuerryArray in the php script.

How can I get access to QuerryArray or any other method in myfunc ^

  3. Re: $db unknown variable in included...   Reply   Report abuse  
Picture of DENIS CARON DENIS CARON - 2014-09-06 08:22:44 - In reply to message 2 from Claude Gélinas
Hi,

Simply add a global call...

$db = new MySQL();
...
$result = myfunc($a,$b);

function myfn($param1,$param2){
global $db;
$sql = "select......";
$res = $obj->QueryArray($sql,MYSQLI_ASSOC);
...
return something;
}


Regards
Denis CARON
SoluNeXT
http://solunext.fr