2 MySQL connections on 2 different hosts


I have 2 connection database (MySQL) in different host like this:

//database 1

//database 2

function dbconnection($dbhost,$dbuser,$dbpass,$dbname){
        echo "Error occure on opening connection or database. Period.";
            echo "Error occure on select databases !";
function dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams){
    $cxn = mysql_connect($dbhostams,$dbuserams,$dbpassams,$dbnameams);
    if( $cxn === FALSE ) {
        die('mysql connection error: '.mysql_error());
        if( !mysql_select_db($dbnameams) ){
            echo "Error occure on select databases !";

when i use:


at my page code, and use:


at another line of code in same page, error occured, like this:

Warning: Access denied for user: '[email protected]' (Using password: NO) in
/home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php on line 17

Warning: MySQL Connection Failed: Access denied for user: '[email protected]'
(Using password: NO) in /home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php
on line 17

mysql connection error:

what must i do to solve this problem?

Your global variables ($dbhost, $dbuser, etc.) are not within the scope of your function calls; for example, if those function calls take place within another function, you will need to declare the variables within the function with the global keyword in order to access them (otherwise PHP thinks you're referring to different variables of the same name that are local to the function):

function foo() {
  global $dbhost, $dbuser, $dbpass, $dbname;

Read more on PHP variable scope.