note 57533 deleted from function.mysql-connect by bjori
| From: | bjori@php.net | Date: | Sat, 15 Apr 2006 18:40:03 +0000 |
| Subject: | note 57533 deleted from function.mysql-connect by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108193@lists.php.net to get a copy of this message | ||
Note Submitter: baldurien at club-internet dot fr
----
There are some case where you will have one user, for example root, that may use different database,
for example :
<?php
$x = mysql_connect( ... );
mysql_select_db('foo', $x);
/* some queries on foo */
mysql_select_db('bar', $x );
/* some queries on bar */
?>
This is fine in that case, however if you would like to transfer one table from database
"foo" to database "bar", you may do that :
INSERT INTO bar.desttable SELECT * FROM foo.srctable
(or CREATE TABLE)
But if you would like to convert data using a specific function, then you will rather use that :
<?php
$x = mysql_connect( 'localhost', 'root', 'root' );
$y = mysql_connect( 'localhost', 'root', 'root' );
mysql_select_db('foo', $x);
mysql_select_db('bar', $y );
$q = mysql_query( 'SELECT field1, ..., fieldN
FROM srctable', $x );
while ( $a = mysql_fetch_row($q))
mysql_query( 'INSERT INTO bar VALUES(\" . foo($a[0]) . '\', ...,
\'' . foo($a[0]) . '\'', $y);
?>
This will fail.
The reason is simple : PHP will reuse the existant connection for the second mysql_connect, so when
you will select a database on server $x, you will also select it for $y, and vice versa!
This is why you have to use the fourth parameter to avoid this :
<?php
$x = mysql_connect( 'localhost', 'root', 'root' );
$y = mysql_connect( 'localhost', 'root', 'root', true );
?>
This way your script will work.
Note : doing that mysql_close on $x will result in closing the connection for $y too.
<?php
$x = mysql_connect( 'localhost', 'root', 'root' );
$y = mysql_connect( 'localhost', 'root', 'root' );
mysql_close($x);
mysql_close($y); // server is already closed
?>