For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > December 2007 > passing a mysqli object via globals









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author passing a mysqli object via globals
julian

2007-12-03, 7:59 am

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
julian

2007-12-03, 7:59 am



however this will work...

p1.inc

1 <?php
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 <?php
4
5 require_once('p1.inc');
6
7 $db= new dbb();
8
9
10 $obj = new obj();
11
12 $obj->f1();
13
14 ?>
Tedder

2007-12-12, 5:57 pm

Ashlee Simpson and Sarah M. Gellar Wrestling & Showing Ass!
http://www.thetubebender.com/Window...peg?clip=726648
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com