<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.dba.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.dba-open.php',
    1 => 'dba_open',
    2 => '打开数据库',
  ),
  'up' => 
  array (
    0 => 'ref.dba.php',
    1 => 'DBA 函数',
  ),
  'prev' => 
  array (
    0 => 'function.dba-nextkey.php',
    1 => 'dba_nextkey',
  ),
  'next' => 
  array (
    0 => 'function.dba-optimize.php',
    1 => 'dba_optimize',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/dba/functions/dba-open.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.dba-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dba_open</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">dba_open</span> &mdash; <span class="dc-title">打开数据库</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.dba-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dba_open</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$path</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$mode</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$handler</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$permission</code><span class="initializer"> = 0644</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$map_size</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="class.dba-connection.php" class="type Dba\Connection">Dba\Connection</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">
   <span class="function"><strong>dba_open()</strong></span> 用 <code class="parameter">mode</code> 和 <code class="parameter">handler</code> 为 <code class="parameter">path</code> 建立一个数据库实例。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.dba-open-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">path</code></dt>
    <dd>
     <span class="simpara">
      数据库文件的路径。通常是文件系统中的一个常规路径。
     </span>
    </dd>
   
   
    <dt><code class="parameter">mode</code></dt>
    <dd>
     <span class="simpara">
      一个字符串，指定数据库的访问模式。它是一个字符，可以是 <code class="literal">r</code> 用于读取访问，
      <code class="literal">w</code> 用于对已存在的数据库进行读写访问，
      <code class="literal">c</code> 用于读写访问和数据，如果目前不存在，则创建数据库，
      以及 <code class="literal">n</code> 用于创建、截断和读写访问。
      数据库是以 BTree 模式创建的，不支持其他模式（如 Hash 或 Queue）。
     </span>
     <span class="simpara">
      此外，您可以使用下一个字符设置数据库锁定方法。使用 <code class="literal">l</code> 用 <var class="filename">.lck</var>
      文件锁定数据库，或使用 <code class="literal">d</code> 锁定数据库文件本身。重要的是您的所有应用程序都要一致地这样做。
     </span>
     <span class="simpara">
      如果您想测试访问并且不想等待锁定，可以将 <code class="literal">t</code> 作为第三个字符添加。当您确信不需要数据库锁定时，
      可以使用 <code class="literal">-</code> 代替 <code class="literal">l</code> 或 <code class="literal">d</code>。当 <code class="literal">d</code>、
      <code class="literal">l</code> 或 <code class="literal">-</code> 都没有使用时，dba 将锁定数据库文件，就像使用 <code class="literal">d</code> 一样。
     </span>
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <p class="para">
       一个数据库文件只能有一个写入者。当您在 web 服务器上使用 dba 时，如果有多个请求需要写入操作，
       它们只能一个接一个地进行。在写入时不允许读取。dba 扩展使用锁来防止这种情况。请参阅下表：
       <table class="doctable table">
        <caption><strong>DBA 锁定</strong></caption>
        
         <thead>
          <tr>
           <th>已经打开</th>
           <th><code class="parameter">mode</code> = &quot;rl&quot;</th>
           <th><code class="parameter">mode</code> = &quot;rlt&quot;</th>
           <th><code class="parameter">mode</code> = &quot;wl&quot;</th>
           <th><code class="parameter">mode</code> = &quot;wlt&quot;</th>
           <th><code class="parameter">mode</code> = &quot;rd&quot;</th>
           <th><code class="parameter">mode</code> = &quot;rdt&quot;</th>
           <th><code class="parameter">mode</code> = &quot;wd&quot;</th>
           <th><code class="parameter">mode</code> = &quot;wdt&quot;</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>未打开</td>
           <td>ok</td>
           <td>ok</td>
           <td>ok</td>
           <td>ok</td>
           <td>ok</td>
           <td>ok</td>
           <td>ok</td>
           <td>ok</td>
          </tr>

          <tr>
           <td><code class="parameter">mode</code> = &quot;rl&quot;</td>
           <td>ok</td>
           <td>ok</td>
           <td>wait</td>
           <td>false</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
          </tr>

          <tr>
           <td><code class="parameter">mode</code> = &quot;wl&quot;</td>
           <td>wait</td>
           <td>false</td>
           <td>wait</td>
           <td>false</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
          </tr>

          <tr>
           <td><code class="parameter">mode</code> = &quot;rd&quot;</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>ok</td>
           <td>ok</td>
           <td>wait</td>
           <td>false</td>
          </tr>

          <tr>
           <td><code class="parameter">mode</code> = &quot;wd&quot;</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>illegal</td>
           <td>wait</td>
           <td>false</td>
           <td>wait</td>
           <td>false</td>
          </tr>

         </tbody>
        
       </table>

       <ul class="simplelist">
        <li>ok: 第二次调用将成功。</li>
        <li>wait: 第二次调用将等待，直到第一个调用 <span class="function"><a href="function.dba-close.php" class="function">dba_close()</a></span>。</li>
        <li>false: 第二次调用返回 false。</li>
        <li>illegal: 不能混合 <code class="literal">&quot;l&quot;</code> 和 <code class="literal">&quot;d&quot;</code> 修饰符用于 <code class="parameter">mode</code> 参数。</li>
       </ul>
      </p>
     </p></blockquote>
    </dd>
   
   
    <dt><code class="parameter">handler</code></dt>
    <dd>
     <span class="simpara">
      用于访问 <code class="parameter">path</code> 的 <a href="dba.requirements.php" class="link">处理器</a> 的名称。
      它接收所有给定给 <span class="function"><strong>dba_open()</strong></span> 的可选参数，
      并可以代表它们执行操作。如果 <code class="parameter">handler</code> 是 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>，则调用默认处理器。
     </span>
    </dd>
   
   
    <dt><code class="parameter">permission</code></dt>
    <dd>
     <span class="simpara">
      可选的 <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 参数，传递给驱动程序。它的含义与 <span class="function"><a href="function.chmod.php" class="function">chmod()</a></span> 的 <code class="parameter">permissions</code> 参数相同，
      默认为 <code class="literal">0644</code>。
     </span>
     <span class="simpara">
      <code class="literal">db1</code>、<code class="literal">db2</code>、<code class="literal">db3</code>、<code class="literal">db4</code>、
      <code class="literal">dbm</code>、<code class="literal">gdbm</code>、<code class="literal">ndbm</code> 和 <code class="literal">lmdb</code>
      驱动程序支持 <code class="parameter">permission</code> 参数。
     </span>
    </dd>
   
   
    <dt><code class="parameter">map_size</code></dt>
    <dd>
     <span class="simpara">
      可选的 <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 参数，传递给驱动程序。它的值应该是 OS 的页面大小的倍数，或者是零，以使用默认的映射大小。
     </span>
     <span class="simpara">
      只有 <code class="literal">lmdb</code> 驱动程序接受 <code class="parameter">map_size</code> 参数。
     </span>
    </dd>
   
   
    <dt><code class="parameter">flags</code></dt>
    <dd>
     <span class="simpara">
      传递给数据库驱动程序的标志。如果 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>，将提供默认标志。
      目前，只有 LMDB 驱动程序支持以下标志
      <strong><code><a href="dba.constants.php#constant.dba-lmdb-use-sub-dir">DBA_LMDB_USE_SUB_DIR</a></code></strong> 和
      <strong><code><a href="dba.constants.php#constant.dba-lmdb-no-sub-dir">DBA_LMDB_NO_SUB_DIR</a></code></strong>。
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.dba-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   成功时返回一个 <span class="classname"><a href="class.dba-connection.php" class="classname">Dba\Connection</a></span> 实例， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.dba-open-errors">
  <h3 class="title">错误／异常</h3>
  <p class="simpara">
   如果 <code class="parameter">handler</code> 为 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>，但没有默认处理器，则返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
   并发出一个 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 级别的错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.dba-open-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.4.0</td>
      <td>
       现在返回一个 <span class="classname"><a href="class.dba-connection.php" class="classname">Dba\Connection</a></span> 实例；
       以前返回一个 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       <code class="parameter">flags</code> 参数被添加。
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       <code class="parameter">handler</code> 现在可以为 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>。
      </td>
     </tr>

     <tr>
      <td>7.3.14, 7.4.2</td>
      <td>
       <code class="literal">lmdb</code> 驱动现在支持额外的 <code class="parameter">map_size</code> 参数。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.dba-open-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.dba-popen.php" class="function" rel="rdfs-seeAlso">dba_popen()</a> - 打开数据库持久化</span></li>
   <li><span class="function"><a href="function.dba-close.php" class="function" rel="rdfs-seeAlso">dba_close()</a> - 关闭 DBA 数据库</span></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>