Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

multiple php-mysql connects?

Codes here !

Moderators: macek, egami, gesf

Re: multiple php-mysql connects?

Postby WiZARD » Thu Jul 11, 2002 4:02 am

ecoiste wrote: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: Select all
$var =& $GLOBALS["var"];
echo ($var);
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby elitecodex » Thu Jul 11, 2002 4:16 am

Im kinda new, but I would try bringing your var $res inside of "class c"...
so your class would look something like this:
Code: Select all
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: Select all
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
elitecodex
New php-forum User
New php-forum User
 
Posts: 68
Joined: Tue Jul 09, 2002 8:45 am
Location: East Coast, USA


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.