1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Two databases

Discussion in 'Databases' started by D-Fraz, Oct 22, 2009.

  1. #1
    I want to connect two databases, but I can't.
    This is what I have:
    <?php
    mysql_connect("localhost","root");
    $mybbDb = mysql_select_db("mybb2");
    //RevBB
    $revbbDb = mysql_select_db("newbb");
    $users = mysql_query(
    	"SELECT `uid`
    	FROM `$mybbDb.myf_users`"
    )or die(mysql_error());
    while($user = mysql_fetch_array($users))
    {
    	echo $user['uid'];
    }
    PHP:
    First off, I'm not using the Newbb database, and I don't need to right yet.
    The problem is, I can't connect to the MyBB2 database. Gives me:
    Incorrect table name '1.myf_users'

    Can someone help me?
    Thanks.
     
    D-Fraz, Oct 22, 2009 IP
  2. donmarcos

    donmarcos Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    assuming that root has granted rights to access both db's you just need to login into one and then on your
    select do something like this :

    SELECT `uid` FROM `mybb2.myf_users

    Your problem is that your are getting the response mysql_db_select call into the var $revbbDb, and usually
    that is 1 or 0 , so you need to have a variable for your databases and use it that way

    kind of

    $dbname = "thedb" ;

    SELECT `uid` FROM `$dbname.myf_users
     
    donmarcos, Oct 23, 2009 IP
  3. pubdomainshost.com

    pubdomainshost.com Peon

    Messages:
    1,277
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #3
    donmarcos is right, when the following line gets executed
    $mybbDb = mysql_select_db("mybb2");
    Code (markup):
    the value 1 is stored in variable $mybbDb thus
    $mybbDb = 1 ;
    Code (markup):
    => when the select query is run
    $users = mysql_query("SELECT `uid` FROM `$mybbDb.myf_users`"
    Code (markup):
    it becomes
    $users = mysql_query("SELECT `uid` FROM `1.myf_users`"
    Code (markup):
    You may create 2 variables
    
    $db1 = mybb2; // and 
    $db2 = newbb; // and 
    
    Code (markup):
    Than create your select query as
    $users = mysql_query("SELECT `uid` FROM `$db1.myf_users`"
    Code (markup):

    Test it and let us know if that works for you!!!
     
    pubdomainshost.com, Oct 23, 2009 IP
  4. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Please replace the line with mybb2.myf_users it will work please dont use variable use database name
     
    nirajkum, Oct 26, 2009 IP
    akki313 likes this.
  5. paul5

    paul5 Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is there an easier code to write in other than HTML or is that the best of best?
     
    paul5, Oct 28, 2009 IP
  6. donmarcos

    donmarcos Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Paul

    what do you mean ?
     
    donmarcos, Oct 28, 2009 IP
  7. IIWizard

    IIWizard Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No HTML here bro...

    HTML is coding, it works with browser to display the images and text that you see.

    PHP, is a server side language....probably has the most support, totally free, pretty much can't go wrong with learning it. PHP is programming (different from coding) as a certain action has to take place by the user for it to do its thing.

    SQL/PHP, would be a great language to learn, but start with HTML/CSS.

    As for the OP, declare both db's as a varibable as it was suggested. It will take care of your problem.
     
    IIWizard, Oct 29, 2009 IP
  8. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #8
    D-Fraz,

    You are using $mybbDb in a wrong way. If both database on the same server and the MySQL user has rights to both database then you can simply run your query as donmarcos suggested. But it is better to use variables instead of actual database names. It will help you in future when you move from one host to another.
     
    mwasif, Nov 1, 2009 IP
  9. popcool

    popcool Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i use 2 phpbb sql db
     
    popcool, Nov 12, 2009 IP