How to use multiple database using php

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

    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

    #2
    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.
    Creator of
    Epix.Mobi

    Keep an Eye on us Big things coming soon!!!!
    Need something for your site hit me up here

    http://coding-talk.com/forum/main-fo...r-your-wapsite

    Comment


      #3
      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 ...
      This is ten percent luck, twenty percent skill
      Fifteen percent concentrated power of will
      Five percent pleasure, fifty percent pain

      And a hundred percent reason to remember the name!

      Comment


        #4
        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.

        Comment

        Working...
        X