Help Working with Multiple DB

Discussion in 'PHP' started by adamjblakey, Mar 31, 2009.

  1. #1
    Hi,

    I am having problems with the following, it wont select the second database.

    
    $db1 = mysql_connect('localhost', 'user', 'pass');
    $sel1 = mysql_select_db('database');
    
    	$result = mysql_query("SELECT * FROM `cms_users`", $db1); 
    
    	while ($row = mysql_fetch_assoc($result)) {
    
    	$db2  = mysql_connect('localhost', 'user', 'pass');  
    	$sel2 = mysql_select_db('database');
    
      	$backup = new iam_backup("localhost", $row['dname'], $row['user'], $row['pass'], true, false, true, "../uploads/backup/file.sql.gz");
    
    	$backup->perform_backup();
    	
    	ftp_upload_backup("../uploads/backup/file.sql.gz");
    
    }
    
    PHP:
    Cheers,
    Adam
     
    adamjblakey, Mar 31, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    ok, try this first.
    $sel1 = mysql_select_db('database_name',$db1);

    $sel2 = mysql_select_db('database_2_name',$db2);

    first it should be two different names of databases. not sure.
    if the above doesn't work you will have to define the db table as well.
    mysql_select_db('database2_name.table_name',$db2);

    and you might get the first step to work but I've had it fail (after working for 3 days!)
     
    shallowink, Mar 31, 2009 IP
  3. Lpe04

    Lpe04 Peon

    Messages:
    579
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You shouldn't have to connect twice. I think there is also a way you can select different db's by doing

    database.tablename
    database2.tablename

    in your queries but don't quote me on that ;)

    Cheers,
     
    Lpe04, Mar 31, 2009 IP
  4. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #4
    Connect #1, Close #1, Connect #2 .. Try this :
    $db1 = mysql_connect('localhost', 'user', 'pass');
    $sel1 = mysql_select_db('database');
    
        $result = mysql_query("SELECT * FROM `cms_users`", $db1);
    
        while ($row = mysql_fetch_assoc($result)) {
    
        mysql_close($db1); // Close the previous connection
    
        $db2  = mysql_connect('localhost', 'user', 'pass'); 
        $sel2 = mysql_select_db('database');
    
        $backup = new iam_backup("localhost", $row['dname'], $row['user'], $row['pass'], true, false, true, "../uploads/backup/file.sql.gz");
    
        $backup->perform_backup();
       
        ftp_upload_backup("../uploads/backup/file.sql.gz");
    
    }
    PHP:
     
    ActiveFrost, Mar 31, 2009 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Hi,

    I have tried all these but none seem to be working. I don't get an error it just runs for about half a second and stops.

    Any other ideas?
    Cheers,
    Adam
     
    adamjblakey, Apr 14, 2009 IP
  6. zendowind

    zendowind Banned

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Any chances to see your Apache error log ? No errors in your browser does not mean that there are no errors at all !
     
    zendowind, Apr 14, 2009 IP