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 6994621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:54:56+00:00 2026-05-27T19:54:56+00:00

I want to select a MySQL database to use after a PHP PDO object

  • 0

I want to select a MySQL database to use after a PHP PDO object has already been created. How do I do this?

// create PDO object and connect to MySQL
$dbh = new PDO( 'mysql:host=localhost;', 'name', 'pass' );

// create a database named 'database_name'

// select the database we just created ( this does not work )
$dbh->select_db( 'database_name' );

Is there a PDO equivalent to mysqli::select_db?

Perhaps I’m trying to use PDO improperly? Please help or explain.

EDIT

Should I not be using PDO to create new databases? I understand that the majority of benefits from using PDO are lost on a rarely used operation that does not insert data like CREATE DATABASE, but it seems strange to have to use a different connection to create the database, then create a PDO connection to make other calls.

  • 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-05-27T19:54:57+00:00Added an answer on May 27, 2026 at 7:54 pm

    Typically you would specify the database in the DSN when you connect. But if you’re creating a new database, obviously you can’t specify that database the DSN before you create it.

    You can change your default database with the USE statement:

    $dbh = new PDO("mysql:host=...;dbname=mysql", ...);
    
    $dbh->query("create database newdatabase");
    
    $dbh->query("use newdatabase");
    

    Subsequent CREATE TABLE statements will be created in your newdatabase.


    Re comment from @Mike:

    When you switch databases like that it appears to force PDO to emulate prepared statements. Setting PDO::ATTR_EMULATE_PREPARES to false and then trying to use another database will fail.

    I just did some tests and I don’t see that happening. Changing the database only happens on the server, and it does not change anything about PDO’s configuration in the client. Here’s an example:

    <?php
    
    // connect to database
    try {
        $pdo = new PDO('mysql:host=huey;dbname=test', 'root', 'root');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    } catch(PDOException $err) {
        die($err->getMessage());
    }
    
    $stmt = $pdo->prepare("select * from foo WHERE i = :i");
    $result = $stmt->execute(array("i"=>123));
    print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
    
    $pdo->exec("use test2");
    
    $stmt = $pdo->prepare("select * from foo2 WHERE i = :i AND i = :i");
    $result = $stmt->execute(array("i"=>456));
    print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
    

    If what you’re saying is true, then this should work without error. PDO can use a given named parameter more than once only if PDO::ATTR_EMULATE_PREPARES is true. So if you’re saying that this attribute is set to true as a side effect of changing databases, then it should work.

    But it doesn’t work — it gets an error “Invalid parameter number” which indicates that non-emulated prepared statements remains in effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use this to get data from row out of mysql database
I want to grab data from a mysql database by using php. The data
I'm having problems updating a mysql database. I want to run this script on
I want to store image on mysql database by php as blob type, but
I want to do a select in MySql that combines several columns... something like
My sql table is something like (message,created) I want to select those rows which
I want to select the topmost element in a document that has a given
I am using prepared statements to execute mysql database queries. And I want to
I have a MySQL database that looks like this: users ( id , name
I want to select a row from mysql which matches a specific id. I

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.