downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/pdo.connections.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/book.pdo.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'pdo.connections.php',
   
1 => 'Connections and Connection management',
  ),
 
'up' =>
  array (
   
0 => 'book.pdo.php',
   
1 => 'PHP Data Objects',
  ),
 
'prev' =>
  array (
   
0 => 'pdo.constants.php',
   
1 => 'Predefined Constants',
  ),
 
'next' =>
  array (
   
0 => 'pdo.transactions.php',
   
1 => 'Transactions and auto-commit',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div>
 <h1>Connections and Connection management</h1>

 <p class="para">
  Connections are established by creating instances of the PDO base class.
  It doesn&#039;t matter which driver you want to use; you always use the PDO
  class name. The constructor accepts parameters for specifying the
  database source (known as the DSN) and optionally for the username and
  password (if any).
 </p>
 <p class="para">
  </p><div class="example">
   <p><b>Example #1 Connecting to MySQL</b></p>
   <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql:host=localhost;dbname=test'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div><p>
 </p>
 <p class="para">
  If there are any connection errors, a <i>PDOException</i>
  object will be thrown.  You may catch the exception if you want to handle
  the error condition, or you may opt to leave it for an application
  global exception handler that you set up via
  <a href="function.set-exception-handler.php" class="function">set_exception_handler()</a>.
 </p>
 <p class="para">
  </p><div class="example"><p><b>Example #2 Handling connection errors</b></p>
   <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql:host=localhost;dbname=test'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;from&nbsp;FOO'</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"Error!:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;die();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div><p>
 </p>
 <div class="warning"><b class="warning">Warning</b>
  <p class="para">
   If your application does not catch the exception thrown from the PDO
   constructor, the default action taken by the zend engine is to terminate
   the script and display a back trace.  This back trace will likely reveal
   the full database connection details, including the username and
   password.  It is your responsibility to catch this exception, either
   explicitly (via a <i>catch</i> statement) or implicitly via
   <a href="function.set-exception-handler.php" class="function">set_exception_handler()</a>.
  </p>
 </div>
 <p class="para">
  Upon successful connection to the database, an instance of the PDO class
  is returned to your script.  The connection remains active for the
  lifetime of that PDO object.  To close the connection, you need to
  destroy the object by ensuring that all remaining references to it are
  deleted--you do this by assigning <b><tt class="constant">NULL</tt></b> to the variable that holds the
  object.  If you don&#039;t do this explicitly, PHP will automatically close
  the connection when your script ends.
 </p>
 <p class="para">
  </p><div class="example">
   <p><b>Example #3 Closing a connection</b></p>
   <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql:host=localhost;dbname=test'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;use&nbsp;the&nbsp;connection&nbsp;here<br /><br /><br />//&nbsp;and&nbsp;now&nbsp;we're&nbsp;done;&nbsp;close&nbsp;it<br /></span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div><p>
 </p>
 <p class="para">
  Many web applications will benefit from making persistent connections to
  database servers.  Persistent connections are not closed at the end of the
  script, but are cached and re-used when another script requests a
  connection using the same credentials.  The persistent connection cache
  allows you to avoid the overhead of establishing a new connection every
  time a script needs to talk to a database, resulting in a faster web
  application.
 </p>
 <p class="para">
  </p><div class="example">
   <p><b>Example #4 Persistent connections</b></p>
   <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql:host=localhost;dbname=test'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">,&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_PERSISTENT&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true<br /></span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div><p>
 </p>
 <blockquote><p><b class="note">Note</b>:
 
   If you wish to use persistent connections, you must set
   <b><tt class="constant">PDO::ATTR_PERSISTENT</tt></b> in the array of driver options
   passed to the PDO constructor. If setting this attribute with
   <a href="pdo.setattribute.php" class="function">PDO::setAttribute()</a> after instantiation of the
   object, the driver will not use persistent connections.
  <br />
 </p></blockquote>
 <blockquote><p><b class="note">Note</b>:
 
   If you&#039;re using the PDO ODBC driver and your ODBC libraries support ODBC
   Connection Pooling (unixODBC and Windows are two that do; there may be
   more), then it&#039;s recommended that you don&#039;t use persistent PDO
   connections, and instead leave the connection caching to the ODBC
   Connection Pooling layer.  The ODBC Connection Pool is shared with other
   modules in the process; if PDO is told to cache the connection, then
   that connection would never be returned to the ODBC connection pool,
   resulting in additional connections being created to service those other
   modules.
  <br />
 </p></blockquote>
</div>
<?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites