File: connections.xml

package info (click to toggle)
php-doc 20081024-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 57,752 kB
  • ctags: 3,858
  • sloc: xml: 686,554; php: 19,446; perl: 610; cpp: 500; makefile: 336; sh: 114; awk: 28
file content (156 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.4 $ -->

<chapter xml:id="pdo.connections" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <title>Connections and Connection management</title>
 <para>
  Connections are established by creating instances of the PDO base class.
  It doesn'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).
 </para>
 <para>
  <example>
   <title>Connecting to MySQL</title>
   <programlisting role='php'>
<![CDATA[
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
]]> 
   </programlisting>
  </example>
 </para>
 <para>
  If there are any connection errors, a <literal>PDOException</literal>
  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
  <function>set_exception_handler</function>.
 </para>
 <para>
  <example><title>Handling connection errors</title>
   <programlisting role='php'>
<![CDATA[
<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    foreach($dbh->query('SELECT * from FOO') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>
]]>
   </programlisting>
  </example>
 </para>
 <warning>
  <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 <literal>catch</literal> statement) or implicitly via
   <function>set_exception_handler</function>.
  </para>
 </warning>
 <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 &null; to the variable that holds the
  object.  If you don't do this explicitly, PHP will automatically close
  the connection when your script ends.
 </para>
 <para>
  <example>
   <title>Closing a connection</title>
   <programlisting role='php'>
<![CDATA[
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// use the connection here


// and now we're done; close it
$dbh = null;
?>
]]>
   </programlisting>
  </example>
 </para>
 <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.
 </para>
 <para>
  <example>
   <title>Persistent connections</title>
   <programlisting role='php'>
<![CDATA[
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));
?>
]]>
   </programlisting>
  </example>
 </para>
 <note>
  <para>
   If you wish to use persistent connections, you must set
   <constant>PDO::ATTR_PERSISTENT</constant> in the array of driver options
   passed to the PDO constructor. If setting this attribute with
   <function>PDO::setAttribute</function> after instantiation of the
   object, the driver will not use persistent connections.
  </para>
 </note>
 <note>
  <para>
   If you'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's recommended that you don'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.
  </para>
 </note>
</chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->