問題描述
我想在創建 PHP PDO 對象后選擇要使用的 MySQL 數據庫.我該怎么做?
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' );
是否有等效于 mysqli::select_db 的 PDO?
Is there a PDO equivalent to mysqli::select_db?
也許我試圖不正確地使用 PDO?請幫助或解釋.
Perhaps I'm trying to use PDO improperly? Please help or explain.
編輯
我不應該使用 PDO 創建新數據庫嗎?我知道使用 PDO 的大部分好處在一個很少使用的操作中丟失了,它不會像 CREATE DATABASE
那樣插入數據,但是不得不使用不同的連接來創建數據庫似乎很奇怪,然后創建 PDO 連接以進行其他調用.
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.
推薦答案
通常,您會在連接時在 DSN 中指定數據庫.但是,如果您正在創建一個新數據庫,顯然您不能在創建之前將該數據庫指定為 DSN.
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.
您可以使用 USE
語句更改默認數據庫:
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");
隨后的 CREATE TABLE
語句將在您的新數據庫中創建.
Subsequent CREATE TABLE
statements will be created in your newdatabase.
來自@Mike 的重新評論:
Re comment from @Mike:
當您像這樣切換數據庫時,它似乎會強制 PDO 模擬準備好的語句.將 PDO::ATTR_EMULATE_PREPARES 設置為 false,然后嘗試使用其他數據庫將失敗.
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.
我只是做了一些測試,但我沒有看到這種情況發生.更改數據庫只發生在服務器上,它不會更改客戶端中 PDO 配置的任何內容.舉個例子:
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));
如果您說的是真的,那么這應該可以正常工作.僅當 PDO::ATTR_EMULATE_PREPARES 為真時,PDO 才能多次使用給定的命名參數.因此,如果您說這個屬性設置為 true 作為更改數據庫的副作用,那么它應該可以工作.
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.
這篇關于如何在 PHP 中選擇要與 PDO 一起使用的 MySQL 數據庫?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!