note 109907 added to function.mysql-connect
| From: | vanhoeyveld_ruben at hotmail dot com | Date: | Sun, 02 Sep 2012 13:06:06 +0000 |
| Subject: | note 109907 added to function.mysql-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190423@lists.php.net to get a copy of this message | ||
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...
----
Server IP: 69.147.83.197
Probable Submitter: 91.177.145.51
----
Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php
Edit -- https://master.php.net/note/edit/109907
Del: integrated -- https://master.php.net/note/delete/109907/integrated
Del: useless -- https://master.php.net/note/delete/109907/useless
Del: bad code -- https://master.php.net/note/delete/109907/bad+code
Del: spam -- https://master.php.net/note/delete/109907/spam
Del: non-english -- https://master.php.net/note/delete/109907/non-english
Del: in docs -- https://master.php.net/note/delete/109907/in+docs
Del: other reasons-- https://master.php.net/note/delete/109907
Reject -- https://master.php.net/note/reject/109907
Search -- https://master.php.net/manage/user-notes.php