multiple php-mysql connects?

G

Guest

Guest
Following (pseudo)code want work, any ideas?

var $res;

class c{
function queryDB($query){
$DB=mysql_connect($host,$usr...);
mysql_select_db($DBname);
$this->res=mysql_query($query);
mysql_close($DB);
}

function get(){
return mysql_get_assoc($this->res);
}
}

class a extends c{
function query($query){
c::queryDB($query);
}
}

class b extends c{
function query($query){
c::queryDB($query);
}
}

now im trying to use these classes in the folowing way:
$a1=new a();
$b1=new b();
$a1->query("SELECT * FROM tableA");
$aElm=$a1->get();
//no prob till here
$b1->query("SELECT * FROM tableB");
$bElm=$b1->get();


now i get no valid sql result or cannot connect to DB errors depending on whether i close the connection or not (//mysql_close())
Where is the bug?
 
ecoiste said:
Following (pseudo)code want work, any ideas?

var $res;

class c{
function queryDB($query){
$DB=mysql_connect($host,$usr...);
mysql_select_db($DBname);
$this->res=mysql_query($query);
mysql_close($DB);
}

function get(){
return mysql_get_assoc($this->res);
}
}

class a extends c{
function query($query){
c::queryDB($query);
}
}

class b extends c{
function query($query){
c::queryDB($query);
}
}

now im trying to use these classes in the folowing way:
$a1=new a();
$b1=new b();
$a1->query("SELECT * FROM tableA");
$aElm=$a1->get();
//no prob till here
$b1->query("SELECT * FROM tableB");
$bElm=$b1->get();


now i get no valid sql result or cannot connect to DB errors depending on whether i close the connection or not (//mysql_close())
Where is the bug?
Hi there!
I think what you not set variables $host,$usr... or using check without SQL query:
Code:
$var =& $GLOBALS["var"];
echo ($var);
 
Im kinda new, but I would try bringing your var $res inside of "class c"...
so your class would look something like this:
Code:
class c
{
   var $res;           // Througout the class, refer to this as $this->ref

   function queryDB($query)
   {
         // Whatever in here
    }

    function get()
    {
         // whatever
    }
}
Im not sure if this will help or not, but cant hurt. Also, you said that it works fine if you dont use the mysql_close()? Maybe you could make the connection resource a variable in the class too, then have a function to close this connection instead of doing it each time you call queryDB
Code:
class c
{
   var $res;           // Througout the class, refer to this as $this->ref

   var $db_conn;

   function queryDB($query)
   {
         // $this->db_conn = mysql_connect(...) or die(mysql_error());
         
    }
 
    function get()
   {
        // Whatever in here
    }

    function close()
    {
         // mysql_close($this->db_conn);
    }
}
Cant promise it will work, but just a couple of ideas. In my opinion, this is where deconstructors would come in handy. Best of luck.

Will
 
Back
Top