0

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

//database 1
$dbhost='localhost';
$dbuser='user1'; 
$dbpass='pass1';
$dbname='dbname1';

//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';  

function dbconnection($dbhost,$dbuser,$dbpass,$dbname){
    if(!mysql_connect($dbhost,$dbuser,$dbpass)){
        echo "Error occure on opening connection or database. Period.";
    }else{
        if(!mysql_select_db($dbname)){
            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()); 
    }else{
        if( !mysql_select_db($dbnameams) ){
            echo "Error occure on select databases !";
        }
    }
}

when i use:

dbconnection($dbhost,$dbuser,$dbpass,$dbname);

at my page code, and use:

dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams);

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

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

Warning: MySQL Connection Failed: Access denied for user: 'apache@localhost'
(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?

eggyal
  • 122,705
  • 18
  • 212
  • 237
irwan
  • 313
  • 1
  • 4
  • 11
  • possible duplicate of [How do you connect to multiple MySQL databases on a single webpage?](http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage) – CharlesB May 16 '12 at 15:26

4 Answers4

0

You need to pass true as fourth parameter to the mysql_connect function.

$database1 = mysql_connect($host1, $user1, $pass1); 
$database2 = mysql_connect($host2, $user2, $pass2, true); 

mysql_select_db('database1', $database1);
mysql_select_db('database2', $database2);

And to query your database use this:

mysql_query('SELECT * FROM table', $database1);
mysql_query('SELECT * FROM table', $database2);

Check this answer here on Stackoverflow....link

Community
  • 1
  • 1
Abenil
  • 1,048
  • 3
  • 12
  • 26
  • If you check the code he provided, he is connecting to two different DB Hosts, one on localhost, and the other on 192.168.16.3 And if you check the error he provided, it is in the username (DB Login Credentials). – sikas May 10 '12 at 09:10
  • i guess he would have got what i want to say without adding numbers to the parameters, but to make you happy, i edited it. – Abenil May 10 '12 at 09:17
0

First of all, change the variable names, it will be easier for you to read the code ... Second, the problem seems to be with the connection, as it is detecting that the username is "apache". Check the variables names in the functions ... change them and try again.

//database 1
$dbhost='localhost';
$dbuser='user1'; 
$dbpass='pass1';
$dbname='dbname1';

//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';  

function dbconnection($_dbhost,$_dbuser,$_dbpass,$_dbname){
    if(!mysql_connect($_dbhost,$_dbuser,$_dbpass)){
        echo "Error occure on opening connection or database. Period.";
    }else{
        if(!mysql_select_db($_dbname)){
            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()); 
    }else{
        if( !mysql_select_db($dbnameams) ){
            echo "Error occure on select databases !";
        }
    }
}
sikas
  • 5,435
  • 28
  • 75
  • 120
  • Did you test this code because with my understanding of global vs local variable scope I don't see a problem with irwan's code. It could be a php.ini setting possibly, however I don't know which one. – Nathan C. May 10 '12 at 09:17
0

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;
  dbconnection($dbhost,$dbuser,$dbpass,$dbname);
}

Read more on PHP variable scope.

eggyal
  • 122,705
  • 18
  • 212
  • 237
  • Scope doesn't look to be an issue as the variables are being passed when the method is called. – codemonkee May 10 '12 at 23:26
  • @Spero.ShiroPetto: My point was that the *calling code* might not have access to the (global?) variables in order to pass anything other than `null` as arguments - which would certainly explain why it appears PHP has resorted to using default values, as suggested by the error message "`Access denied for user: 'apache@localhost' (Using password: NO)`". – eggyal May 10 '12 at 23:27
0

I didn't see anything obviously wrong, I tested your code as provided and both connections worked.

Check to see if mysql.default_user, mysql.default_host etc. is set within your php.ini

codemonkee
  • 2,881
  • 1
  • 25
  • 32