problem with php adodb, overwrite connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

problem with php adodb, overwrite connection

Post by yacahuma »

Code: Select all


    $host = 'xxx';
    $username='xxx';
    $password ='xxx';
    
    $database1 = 'db1';
    $database2 = 'db2';
    
    $cn1 = NewADOConnection('mysql');
    $cn1->SetFetchMode(ADODB_FETCH_ASSOC);
    $cn1->Connect($host, $username, $password,$database1);

    $cn2 = NewADOConnection('mysql');
    $cn2->SetFetchMode(ADODB_FETCH_ASSOC);
    $cn2->Connect($host, $username, $password,$database2);

    $sql = "UPDATE table1 set msg='XXX', filename='',prtstatus = 'F',dt_finished=NOW() WHERE id_queue='44'";
    $cn1->Execute($sql);

The problem is that $cn1 is now $cn2. You will expect $cn1 to be connected to db1 and not db2. How do I know? Table1 does not exist in db2 and when I execute I get the error saying the table does not exist. If I comment $cn2, everything works.
Last edited by yacahuma on Mon Oct 04, 2010 4:09 pm, edited 3 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: problem with php adodb

Post by John Cartwright »

Do you want me to delete this thread -- or are you actually going to post something?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: problem with php adodb, overwrite connection

Post by yacahuma »

sorry, I submitted by mistake, before the message was finished. Now is complete
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: problem with php adodb, overwrite connection

Post by yacahuma »

found the problem, ooops

Connection Gotcha: If you create two connections, but both use the same userid and password, PHP will share the same connection. This can cause problems if the connections are meant to different databases. The solution is to always use different userid's for different databases, or use NConnect().
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: problem with php adodb, overwrite connection

Post by John Cartwright »

Interesting. I never knew that.
Post Reply