passing a mysqli object via globals
passing a mysqli object via globals
am 03.12.2007 10:18:42 von Julian
warn: already posted in general.... with no response... maybe is too
basic.. but I RTFM and still is not clear to me what I am doing wrong....
I want to open a database connection at program initialization and use
that very same connection via globals initilizating ( loading from db)
other objects.
Basically
index.php
require_once ('file.php');
$db=new mysqli(host,us,pass,db);
if(mysqli_connect_errno()){
die('errordb conex');
}
switch ($var){
display1:
$obj=new obj_x();
$obj->load();
$obj->display();
break;
display2:
$obj=new obj_y();
$obj->load();
$obj->display();
break;
}
------------------
class file.php
class obj_x{
function load(){
globals $db;
$sql="select ....);
$ret=$db->query($sql); // XX
...
}
}
fails at XX with Warning: mysqli::query() [function.mysqli-query]:
Couldn't fetch mysqli in class file .... line XX
Any hints ??. I am using php 5.1.6 with linux/ubuntu.
Thanks.
JCG
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: passing a mysqli object via globals
am 03.12.2007 10:51:37 von Julian
however this will work...
p1.inc
1
2
3 class dbb{
4
5 var $var=10;
6
7 function fun2(){
8 return $this->var;;
9 }
10 }
11
12 class obj {
13 var $obj2=20;
14
15 function f1(){
16 global $db;
17
18 echo "\n".$db->fun2()*$this->obj2."\n";
19 }
20 }
21 ?>
1 #!/usr/bin/php -q
2
3
4
5 require_once('p1.inc');
6
7 $db= new dbb();
8
9
10 $obj = new obj();
11
12 $obj->f1();
13
14 ?>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: passing a mysqli object via globals
am 03.12.2007 16:43:21 von Dee Ayy
Hopefully your only issue is the keyword global versus globals ?
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: passing a mysqli object via globals
am 03.12.2007 17:26:43 von Julian
Dee Ayy wrote:
> Hopefully your only issue is the keyword global versus globals ?
Not sure what you mean. global $db should bring to local scope a
reference to the object that has the data base connection.
PHP complaints that it cannot access properties or methods of that
object in the obj/f1/ scope....
I tried using the $GLOBALS['db'] with same results.....
really lost...
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: passing a mysqli object via globals
am 03.12.2007 17:48:09 von Dee Ayy
On Dec 3, 2007 10:26 AM, julian wrote:
> Dee Ayy wrote:
> > Hopefully your only issue is the keyword global versus globals ?
>
>
> Not sure what you mean. global $db should bring to local scope a
> reference to the object that has the data base connection.
>
> PHP complaints that it cannot access properties or methods of that
> object in the obj/f1/ scope....
>
> I tried using the $GLOBALS['db'] with same results.....
In the one that failed, you used the invalid keyword "globals".
In the one that worked, you used the valid keyword "global".
When I said "hopefully", I was wondering if you could use
$GLOBALS['db'] but there may be other issues since you said that also
failed.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: passing a mysqli object via globals
am 03.12.2007 21:58:32 von Julian
Dee Ayy wrote:
> On Dec 3, 2007 10:26 AM, julian wrote:
>> Dee Ayy wrote:
>>> Hopefully your only issue is the keyword global versus globals ?
>>
>> Not sure what you mean. global $db should bring to local scope a
>> reference to the object that has the data base connection.
>>
>> PHP complaints that it cannot access properties or methods of that
>> object in the obj/f1/ scope....
>>
>> I tried using the $GLOBALS['db'] with same results.....
>
> In the one that failed, you used the invalid keyword "globals".
> In the one that worked, you used the valid keyword "global".
>
> When I said "hopefully", I was wondering if you could use
> $GLOBALS['db'] but there may be other issues since you said that also
> failed.
no worries.... I give up. It is really weird I know.... Hope some day I
will get enlighted.
In one file, any method within a class is happy with the global
$db...on a different (same file !!!) class no method is happy with
accessing $db via global...
I will open and close db connection in each method that it needs...
awkward...but works.
Thanks for your time.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: passing a mysqli object via globals
am 03.12.2007 22:38:51 von Dee Ayy
> no worries.... I give up. It is really weird I know.... Hope some day I
> will get enlighted.
>
> In one file, any method within a class is happy with the global
> $db...on a different (same file !!!) class no method is happy with
> accessing $db via global...
So you confirmed that you are using "global" instead of what you
posted to the list as "globals"?
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: passing a mysqli object via globals
am 04.12.2007 00:21:01 von dmagick
julian wrote:
> warn: already posted in general.... with no response... maybe is too
> basic.. but I RTFM and still is not clear to me what I am doing wrong....
>
>
> I want to open a database connection at program initialization and use
> that very same connection via globals initilizating ( loading from db)
> other objects.
>
> Basically
>
> index.php
>
> require_once ('file.php');
>
> $db=new mysqli(host,us,pass,db);
The order of this stuff matters.
$db isn't set up when you include file.php, hence why it's not an object
when you try to use it.
move the $db = .. line above the file.php reference.
[ I'm sure you would have received a notice or warning or something if
you had error_reporting set to E_ALL and display errors on ]
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: passing a mysqli object via globals
am 04.12.2007 10:43:57 von Julian
Dee Ayy wrote:
>> no worries.... I give up. It is really weird I know.... Hope some day I
>> will get enlighted.
>>
>> In one file, any method within a class is happy with the global
>> $db...on a different (same file !!!) class no method is happy with
>> accessing $db via global...
>
>
> So you confirmed that you are using "global" instead of what you
> posted to the list as "globals"?
yep sorry, as I mention it was a typo.. too upset to type everything
correctly.
However, it behaves the same way if I use $GLOBALS['db'].
I feel fresh and calm today... I will give it another try, separating
classes in different files and do some other teawking ... at least for
the time others are putting.
Cheers.
JCG
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: passing a mysqli object via globals
am 04.12.2007 10:47:34 von Julian
Chris wrote:
> julian wrote:
>> warn: already posted in general.... with no response... maybe is too
>> basic.. but I RTFM and still is not clear to me what I am doing
>> wrong....
>>
>>
>> I want to open a database connection at program initialization and use
>> that very same connection via globals initilizating ( loading from db)
>> other objects.
>>
>> Basically
>>
>> index.php
>>
>> require_once ('file.php');
>>
>> $db=new mysqli(host,us,pass,db);
>
> The order of this stuff matters.
>
> $db isn't set up when you include file.php, hence why it's not an object
> when you try to use it.
>
> move the $db = .. line above the file.php reference.
>
> [ I'm sure you would have received a notice or warning or something if
> you had error_reporting set to E_ALL and display errors on ]
>
Hi Chris,
Thanks for your comments. I do have E_ALL set.
Your view does no explain why in one class it does work and in the other
class it does not. Both classes defined in same file.
However, I will try later in the day you idea and will post results.
many thanks.
JCG
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
I am a bloody idiot
am 04.12.2007 13:22:25 von Julian
Dee Ayy wrote:
>> no worries.... I give up. It is really weird I know.... Hope some day I
>> will get enlighted.
>>
>> In one file, any method within a class is happy with the global
>> $db...on a different (same file !!!) class no method is happy with
>> accessing $db via global...
>
>
> So you confirmed that you are using "global" instead of what you
> posted to the list as "globals"?
So yes...... after so many tests, copying / pasting comenting /
cleaning...somewhere buried there was a $db->close()... which afterwards
was rendering the db connection useless.......thus following classes
were not able to use the object even though it was there.
my apologies.
JCG
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: I am a bloody idiot
am 05.12.2007 00:12:07 von dmagick
julian wrote:
> Dee Ayy wrote:
>>> no worries.... I give up. It is really weird I know.... Hope some day I
>>> will get enlighted.
>>>
>>> In one file, any method within a class is happy with the global
>>> $db...on a different (same file !!!) class no method is happy with
>>> accessing $db via global...
>>
>>
>> So you confirmed that you are using "global" instead of what you
>> posted to the list as "globals"?
>
>
> So yes...... after so many tests, copying / pasting comenting /
> cleaning...somewhere buried there was a $db->close()... which afterwards
> was rendering the db connection useless.......thus following classes
> were not able to use the object even though it was there.
Heh everyone has done that at some time I'm sure :) My next suggestion
was going to be to make sure you're not overwriting or using the $db
variable somewhere in your code (which then affects the global $db
variable)... Another pretty common mistake.
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php