Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:09:36+00:00 2026-06-13T11:09:36+00:00

Currently I connect to database server by this @$db_connect = mysql_connect(‘localhost’,’user’,’xxx’, true); I have

  • 0

Currently I connect to database server by this

@$db_connect = mysql_connect('localhost','user','xxx', true);

I have to connect to database server (not specific database name) due to query with multiple database in the same query.

Can anyone suggest me how to do the same thing with PDO?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:09:37+00:00Added an answer on June 13, 2026 at 11:09 am

    You will still need to choose one of the databases in the PDO $dsn, even if you are to be querying across multiple databases. This isn’t really a problem because in your queries you will use the pattern dbname.tablename.columnname. When querying against only the database actually specified in the $dsn, you don’t need to use the dbname.

    You will, of course, need privileges granted on all of the databases you intend to use for the user specified in the connection.

    // Need more than just testdb, but specify it here anyway
    // It doesn't matter which one you choose here- pick the one you'll use the most I suppose
    $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    
    // Query across 2 databases, and specify aliases for the dbname.tablename
    $stmt = $dbh->prepare("
      SELECT 
        tdb.col1, 
        other.col2 
      FROM 
        testdb.table1.col1 AS tdb
        JOIN otherdb.table2.col2 AS other 
          ON tdb.col1 = other.col2
    ");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
I currently have a PHP form that uses AJAX to connect to MySQL and
Currently, I have an application that uses Firebird in embedded mode to connect to
I have written a database class(es) sometime ago, I use this for most projects
I currently have a PHP file which will search my MySQL database and see
I have xml files been imported onto an FTP server. This is stored in
Greetings Stackoverflow. I am currently using 000webhost.com as my MySQL database / server provider.
I am running a MySQL database with Mac OS X Lion Server. I have
I am trying to connect to my MySQL database on my online server via
I am currently in a condition not to find any errors in this line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.