• <legend id='VE59m'><style id='VE59m'><dir id='VE59m'><q id='VE59m'></q></dir></style></legend>

    <small id='VE59m'></small><noframes id='VE59m'>

      <bdo id='VE59m'></bdo><ul id='VE59m'></ul>
    <tfoot id='VE59m'></tfoot>
    <i id='VE59m'><tr id='VE59m'><dt id='VE59m'><q id='VE59m'><span id='VE59m'><b id='VE59m'><form id='VE59m'><ins id='VE59m'></ins><ul id='VE59m'></ul><sub id='VE59m'></sub></form><legend id='VE59m'></legend><bdo id='VE59m'><pre id='VE59m'><center id='VE59m'></center></pre></bdo></b><th id='VE59m'></th></span></q></dt></tr></i><div id='VE59m'><tfoot id='VE59m'></tfoot><dl id='VE59m'><fieldset id='VE59m'></fieldset></dl></div>

      1. 如何在 PHP 中选择要与 PDO 一起使用的 MySQL 数据库

        时间:2023-09-20
            <bdo id='O8Ybv'></bdo><ul id='O8Ybv'></ul>
                <i id='O8Ybv'><tr id='O8Ybv'><dt id='O8Ybv'><q id='O8Ybv'><span id='O8Ybv'><b id='O8Ybv'><form id='O8Ybv'><ins id='O8Ybv'></ins><ul id='O8Ybv'></ul><sub id='O8Ybv'></sub></form><legend id='O8Ybv'></legend><bdo id='O8Ybv'><pre id='O8Ybv'><center id='O8Ybv'></center></pre></bdo></b><th id='O8Ybv'></th></span></q></dt></tr></i><div id='O8Ybv'><tfoot id='O8Ybv'></tfoot><dl id='O8Ybv'><fieldset id='O8Ybv'></fieldset></dl></div>

                <small id='O8Ybv'></small><noframes id='O8Ybv'>

                <tfoot id='O8Ybv'></tfoot>
                  <tbody id='O8Ybv'></tbody>
                <legend id='O8Ybv'><style id='O8Ybv'><dir id='O8Ybv'><q id='O8Ybv'></q></dir></style></legend>

                1. 本文介绍了如何在 PHP 中选择要与 PDO 一起使用的 MySQL 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在创建 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模板网!

                  上一篇:PHP PDO bindParam 陷入了 foreach 下一篇:PHP PDO 的prepared statements 如何防止sql注入?使用 P

                  相关文章

                  最新文章

                  <tfoot id='i9tCw'></tfoot>

                    <small id='i9tCw'></small><noframes id='i9tCw'>

                  1. <legend id='i9tCw'><style id='i9tCw'><dir id='i9tCw'><q id='i9tCw'></q></dir></style></legend>
                  2. <i id='i9tCw'><tr id='i9tCw'><dt id='i9tCw'><q id='i9tCw'><span id='i9tCw'><b id='i9tCw'><form id='i9tCw'><ins id='i9tCw'></ins><ul id='i9tCw'></ul><sub id='i9tCw'></sub></form><legend id='i9tCw'></legend><bdo id='i9tCw'><pre id='i9tCw'><center id='i9tCw'></center></pre></bdo></b><th id='i9tCw'></th></span></q></dt></tr></i><div id='i9tCw'><tfoot id='i9tCw'></tfoot><dl id='i9tCw'><fieldset id='i9tCw'></fieldset></dl></div>

                      • <bdo id='i9tCw'></bdo><ul id='i9tCw'></ul>