#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171

    Php query multiple databases in single query


    Hi;

    How can I simply query different tables in different databases that are on the same server?

    Thanks


    edit:
    Is this reliable?
    PHP Code:
    mysql_connect('localhost','db_user','pssword');
    mysql_query('SELECT * FROM database_name.table_name'); 
    Last edited by English Breakfast Tea; October 3rd, 2013 at 12:33 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,987
    Rep Power
    375
    doesnt mysql_query takes a second parameter identifying connection resource.. so you could connect to diff database ie

    $conn1 = connect_db;
    $conn2 =..... ;

    you get the idea
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    mysqli functions all accept a database resource. If you don't pass one, they use the most recently created one. Create two resources, and pass them into the functions as appropriate.

    And remember mysql is deprecated in favor of mysqli, but the PDO monster may get you if you don't go all the way.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,988
    Rep Power
    533
    Originally Posted by ManiacDan
    but the PDO monster may get you if you don't go all the way
    What do you mean?
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    Originally Posted by NotionCommotion
    What do you mean?
    We have members who think going to mysqli is stupid because you may as well go all the way to PDO for security reasons.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,988
    Rep Power
    533
    Originally Posted by ManiacDan
    We have members who think going to mysqli is stupid because you may as well go all the way to PDO for security reasons.
    In addition to security benefits, PDO offers better cross platform support and I feel easier to read and maintain script. I don't know if I would use the word "stupid", but... Am I wrong?
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    PDO has no security benefits over MySQLi whatsoever. In fact, it's somewhat less secure, because if you don't know what you're doing, you'll be running around with those "emulated prepared statements". And that's a problem.

    Personally, however, I still go with PDO, because I don't like fumbling with low-level methods like bind() and fetch(). And MySQL is not the only database system I use.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,988
    Rep Power
    533
    Originally Posted by Jacques1
    PDO has no security benefits over MySQLi whatsoever. In fact, it's somewhat less secure, because if you don't know what you're doing, you'll be running around with those "emulated prepared statements".
    If you don't know what you are doing, you probably will be in more trouble without some rudimentary PDO knowledge. And thanks to you, I no longer use emulated prepared statements (and if others change also, make sure you compiled PHP with the correct libraries or you will have a host of phantom errors!).
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    183
    Rep Power
    83
    Originally Posted by English Breakfast Tea
    Hi;

    How can I simply query different tables in different databases that are on the same server?

    Thanks


    edit:
    Is this reliable?
    PHP Code:
    mysql_connect('localhost','db_user','pssword');
    mysql_query('SELECT * FROM database_name.table_name'); 
    You can do that if the user has privilege on both / all databases
    PHP Code:
    mysql_connect('localhost','db_user','pssword');
    mysql_query('SELECT * FROM database_name.table_name d1 JOIN database2.table_name d2 on d1.id = d2.id'); 
  18. #10
  19. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171
    Originally Posted by BarryG
    You can do that if the user has privilege on both / all databases
    PHP Code:
    mysql_connect('localhost','db_user','pssword');
    mysql_query('SELECT * FROM database_name.table_name d1 JOIN database2.table_name d2 on d1.id = d2.id'); 
    Hi Barry it's all working fine

    Its a cron job that updates orders etc.

IMN logo majestic logo threadwatch logo seochat tools logo