note 109907 deleted from function.mysql-connect by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 01:16:11 +0000 |
| Subject: | note 109907 deleted from function.mysql-connect by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191682@lists.php.net to get a copy of this message | ||
Note Submitter: vanhoeyveld_ruben at hotmail dot com
----
When connecting to multiple databases and using several different identifiers, pay attention to the
fact that those identifiers cannot be found from within a function unless they're passed as a
parameter in this function.
Example:
<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$tablename = "db";
$identifier_1 = mysql_connect($host,$user,$pass) ;
mysql_select_db($tablename, $identifier_1);
$host = "localhost";
$user = "user2"; // this is another connection than the one above. It has a different
user, pass and table name.
$pass = "pass2";
$tablename = "db2";
$identifier_2 = mysql_connect($host,$user,$pass) ;
mysql_select_db($tablename, $identifier_2);
?>
This won't work:
<?php
function dosomequery(){
$query = mysql_query("SELECT * FROM table", $identifier_1);
//play with this query
}
dosomequery(); // WON'T WORK
?>
This is how to fix:
<?php
function dosomequery($rid){
$query = mysql_query("SELECT * FROM table", $rid);
//play with this query
}
dosomequery($identifier_1); // WILL WORK
?>
As you can see, I passed the identifier to the function as a parameter so the function knows the
identifier. Otherwise, the function can't find it...