note 22749 added to function.mysql-connect

From: Date: Fri, 28 Jun 2002 12:01:16 +0000
Subject: note 22749 added to function.mysql-connect
Groups: php.notes 
Request: Send a blank email to php-notes+get-32364@lists.php.net to get a copy of this message
If you use mysql_connect with identical parameters twice (e.g. a second connection to log all sql queries without an impact on the first one), the resource variables will be different (echo $dbl says Resource id #1, echo $dbl_log says Resource id #2), but in some way the same (mysql_insert_id($dbl) and mysql_insert_id($dbl_log) are always identical). Workaround for those using PHP < 4.2.0 (4.2.0 provides the new_link parameter to solve this problem): use a separate db-user for the second connection I encountered this problem with PHP Version 4.0.6. Example: $dbl = @mysql_connect($dbms_hn, $dbms_un, $dbms_pw); $dbl_log = @mysql_connect($dbms_hn, dbms_un, $dbms_pw); mysql_select_db($dbms_db, $dbl); mysql_select_db($dbms_db, $dbl_log); $sql = ... insert into table-1-with-autoincrement-column statement; $rs = mysql_query($sql, $dbl); echo "dbl: " . $dbl . " insert_id: " . mysql_insert_id($dbl) . '<br/>'; echo "dbl_log: " . $dbl_log . " insert_id: " . mysql_insert_id($dbl_log) . '<br/>'; $sql = ... insert into table-2-with-autoincrement-column statement; $rs = mysql_query($sql, $dbl_log); echo "dbl: " . $dbl . " insert_id: " . mysql_insert_id($dbl) . '<br/>'; echo "dbl_log: " . $dbl_log . " insert_id: " . mysql_insert_id($dbl_log) . '<br/>'; Result: (table1 initially contained 3 rows) dbl: Resource id #1 mysql_insert_id: 4 dbl_log: Resource id #2 mysql_insert_id: 4 dbl: Resource id #1 mysql_insert_id: 1 dbl_log: Resource id #2 mysql_insert_id: 1 This should be the result: dbl: Resource id #1 mysql_insert_id: 4 dbl_log: Resource id #2 mysql_insert_id: 0 dbl: Resource id #1 mysql_insert_id: 4 dbl_log: Resource id #2 mysql_insert_id: 1 --- -- http://www.php.net/manual/en/function.mysql-connect.php http://master.php.net/manage/user-notes.php?action=edit+22749 http://master.php.net/manage/user-notes.php?action=delete+22749 http://master.php.net/manage/user-notes.php?action=reject+22749

« previous php.notes (#32364) next »