How to use multiple database using php

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • MuzicStar
    replied
    Code:
    <?php
    $dbh1 = mysql_connect($hostname, $username, $password);
    $dbh2 = mysql_connect($hostname, $username, $password, true);
    $hostname="localhost";
    $username="star_zedge";
    $password="";
    
    mysql_select_db('star_mstar', $dbh1);
    mysql_select_db('star_admin', $dbh2);  
    $sql = mysql_query('select * from mobile', $dbh2); 
    //$sql = mysql_query ("SELECT * FROM mobile ORDER BY id DESC limit 5",$dbh1);
    $result = mysql_query($sql) or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {  
    
    	echo"<br/>";
    	echo"{$row['mname']}";
            echo"<br/>";
    }
    ?>
    i have try this code but this code also not working.

    Added after 21 minutes:

    Code:
    <?php
    $conn1 = mysql_connect("localhost","USERNAME","PASSWORD") or die(mysql_error());
    $conn2 = mysql_connect("localhost","USERNAME","PASSWORD","true") or die(mysql_error());
    
    mysql_select_db("DB NAME1",$conn1);
    mysql_select_db("DB NAME2",$conn2);
    
    $query = "SELECT * FROM mobile";
    $result = mysql_query($query,$conn1);
    while ($row = mysql_fetch_assoc($result)) {  
    
    	echo"<hr/>SHOW FROM 1ST DATABASE<br/>";
    	echo"{$row['mname']}";
            echo"<br/>";
    }
    $result2 = mysql_query($query,$conn2);
    while ($row = mysql_fetch_assoc($result2)) {  
    
    	echo"<hr/>SHOW FROM 2ND DATABASE<br/>";
    	echo"{$row['mname']}";
            echo"<br/>";
    }
    ?>
    This code working fine
    Thankx for helping me.
    Last edited by MuzicStar; 16.04.12, 07:16.

    Leave a comment:


  • just_m3.
    replied
    Originally posted by Loony View Post
    $dbh2 = mysql_connect($hostname, $username, $password, true);
    found this on google copy and pasted only took 4 minutes.
    yup "true" allows you to connect different database's in same script , without it .. connect cannot be done , because data's cannot be differentiate !

    stackoverflow ...

    Leave a comment:


  • Loony
    replied
    You can make multiple calls to mysql_connect(), but if the parameters are the same you need to pass true for the '$new_link' (fourth) parameter, otherwise the same connection is reused.

    so then you have

    PHP Code:
    $dbh1 mysql_connect($hostname$username$password); 
    $dbh2 mysql_connect($hostname$username$passwordtrue); 

    mysql_select_db('database1'$dbh1);
    mysql_select_db('database2'$dbh2); 
    Then to query database 1, do
    PHP Code:
    mysql_query('select * from tablename'$dbh1); 
    and for database 2

    PHP Code:
    mysql_query('select * from tablename'$dbh2); 
    found this on google copy and pasted only took 4 minutes.
    Last edited by Loony; 15.04.12, 12:08.

    Leave a comment:


  • MuzicStar
    started a topic How to use multiple database using php

    How to use multiple database using php

    plzz php/mysql master help me!!!
    Code:
    <?php 
        $conn = mysql_connect ("localhost", "root", ""); 
        $db1  = mysql_select_db ("database1", $conn); 
        $db2  = mysql_select_db ("database2", $conn); 
    
        $result = mysql_query ("SELECT * FROM whatever", $db1); 
    ?>
    but i got this error msg: Access denied for user 'star'@'localhost' (using password: NO)
    plz correct this coding.
    thankx in advance
Working...
X