File: PDO-construct.xml

package info (click to toggle)
phpdoc 20050512-1
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 36,592 kB
  • ctags: 1,501
  • sloc: xml: 376,768; php: 6,708; cpp: 500; makefile: 293; perl: 161; sh: 151; awk: 28
file content (297 lines) | stat: -rw-r--r-- 9,750 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?xml version='1.0' encoding='iso-8859-1'?>
<!-- $Revision: 1.11 $ -->
  <refentry id="function.PDO-construct">
   <refnamediv>
    <refname>PDO::__construct</refname>
    <refpurpose>
     Creates a PDO instance representing a connection to a database
    </refpurpose>
   </refnamediv>
   <refsect1 role="description">
    &reftitle.description;
    <methodsynopsis>
     <type>PDO</type><methodname>PDO::__construct</methodname>
     <methodparam><type>string</type><parameter>dsn</parameter></methodparam>
     <methodparam choice="opt"><type>string</type><parameter>username</parameter></methodparam>
     <methodparam choice="opt"><type>string</type><parameter>password</parameter></methodparam>
     <methodparam choice="opt"><type>array</type><parameter>driver_options</parameter></methodparam>
    </methodsynopsis>
    &warn.experimental.func;
    <para>
     Creates a PDO instance to represent a connection to the requested
     database. 
    </para>
   </refsect1>
   <refsect1 role="parameters">
    &reftitle.parameters;
    <para>
     <variablelist>
      <varlistentry><term>dsn</term>
       <listitem>
        <para>
         The Data Source Name, or DSN, contains the information required to
         connect to the database.
        </para>
        <para>
         In general, a DSN consists of the PDO driver name, followed by a colon,
         followed by the PDO driver-specific connection syntax. Examples of each
         driver are given below:
        </para>
        <para>
         <variablelist>
          <varlistentry>
           <term><constant>PDO_DBLIB</constant></term>
           <listitem>
            <para>
             The DSN prefix is either <userinput>sybase:</userinput>
             or <userinput>mssql:</userinput> depending on which libraries
             it was linked against during compilation.
            </para>
            <para><userinput>sybase:host=localhost; dbname=testdb</userinput></para>
            <para><userinput>mssql:host=localhost; dbname=testdb</userinput></para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term><constant>PDO_FIREBIRD</constant></term>
           <listitem>
            <para><userinput>firebird:User=john;Password=mypass;Database=DATABASE.GDE;DataSource=localhost;Port=3050</userinput></para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term><constant>PDO_MYSQL</constant></term>
           <listitem>
            <para><userinput>mysql:host=localhost;dbname=testdb</userinput></para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term><constant>PDO_OCI</constant></term>
           <listitem>
            <para>To connect via <filename>tnsnames.ora</filename>, use:</para>
            <para><userinput>oci:mydb</userinput></para>
            <para>If using instantclient, use:</para>
            <para><userinput>oci:dbname=//localhost:1521/testdb</userinput></para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term><constant>PDO_ODBC</constant></term>
           <listitem>
            <para><userinput>odbc:DSN=SAMPLE;UID=john;PWD=mypass</userinput></para>
            <para>DSN=SAMPLE refers to the SAMPLE data source configured in the ODBC driver manager.</para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term><constant>PDO_PGSQL</constant></term>
           <listitem>
            <para><userinput>pgsql:host=localhost port=5432 dbname=testdb user=john password=mypass</userinput></para>
            <para>
             Note, by passing <varname>user</varname> and <varname>password</varname>
             in the DSN, the <parameter>username</parameter>
             and <parameter>password</parameter> parameters become optional.
             If specified, they are glued to the end of the connection string.
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term><constant>PDO_SQLITE</constant></term>
           <listitem>
            <para><userinput>sqlite:/path/to/database</userinput></para>
            <para>To create a database in memory, use:</para>
            <para><userinput>sqlite::memory:</userinput></para>
           </listitem>
          </varlistentry>
         </variablelist>
        </para>
        <para>
         The <parameter>dsn</parameter> parameter supports three
         different methods of specifying the arguments required to create
         a database connection:
        </para>
        <para>
         <variablelist>
          <varlistentry><term>Driver invocation</term>
           <listitem>
            <para>
             <parameter>dsn</parameter> contains the full DSN.
            </para>
           </listitem>
          </varlistentry>
          <varlistentry><term>URI invocation</term>
           <listitem>
            <para>
             <parameter>dsn</parameter> consists of <userinput>uri:</userinput>
             followed by a URI that defines the location of a file containing
             the DSN string. The URI can specify a local file or a remote URL.
            </para>
            <para><userinput>uri:file:///path/to/dsnfile</userinput></para>
           </listitem>
          </varlistentry>
          <varlistentry><term>Aliasing</term>
           <listitem>
            <para>
             <parameter>dsn</parameter> consists of a name 
             <parameter>name</parameter> that maps to 
             <literal>pdo.dsn.<parameter>name</parameter></literal> in &php.ini;
             defining the DSN string.
            </para>
            <note>
             <para>
              The alias must be defined in &php.ini;, and not &htaccess; or &httpd.conf;
             </para>
            </note>
           </listitem>
          </varlistentry>
         </variablelist>
        </para>
       </listitem>
      </varlistentry>
      <varlistentry><term>username</term>
       <listitem>
        <para>
         The user name for the DSN string. This parameter is optional for
         some PDO drivers.
        </para>
       </listitem>
      </varlistentry>
      <varlistentry><term>password</term>
       <listitem>
        <para>
         The password for the DSN string. This parameter is optional for
         some PDO drivers.
        </para>
       </listitem>
      </varlistentry>
      <varlistentry><term>driver_options</term>
       <listitem>
        <para>
         A key=&gt;value array of driver-specific connection options.
        </para>
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </refsect1>

   <refsect1 role="returnvalues">
    &reftitle.returnvalues;
    <para>
     Returns a PDO object on success.
    </para>
   </refsect1>

   <refsect1 role="exceptions">
    &reftitle.exceptions;
    <para>
     <function>PDO::construct</function> throws a PDOException if the attempt
     to connect to the requested database fails.
    </para>
   </refsect1>

   <refsect1 role="examples">
    &reftitle.examples;
    <para>
     <example><title>Create a PDO instance via driver invocation</title>
      <programlisting role="php">
<![CDATA[
<?php
// Connect to an ODBC database using driver invocation
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>
]]>
      </programlisting>
     </example>
     <example><title>Create a PDO instance via URI invocation</title>
      <para>
       The following example assumes that the file
       <filename>/usr/local/dbconnect</filename> exists with file permissions
       that enable PHP to read the file. The file contains the PDO DSN to
       connect to a DB2 database through the PDO_ODBC driver:
      </para>
      <screen>
<![CDATA[
odbc:DSN=SAMPLE;UID=john;PWD=mypass
]]>
      </screen>
      <para>
       The PHP script can then create a database connection by simply
       passing the <literal>uri:</literal> parameter and pointing to
       the file URI:
      </para>
      <programlisting role="php">
<![CDATA[
<?php
// Connect to an ODBC database using driver invocation
$dsn = 'uri:file:///usr/local/dbconnect';
$user = '';
$password = '';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>
]]>
      </programlisting>
     </example>
     <example><title>Create a PDO instance using an alias</title>
      <para>
       The following example assumes that &php.ini; contains the following
       entry to enable a connection to a MySQL database using only the
       alias <literal>mydb</literal>:
       <programlisting role="ini">
[PDO]
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost"
       </programlisting>
      </para>
      <programlisting role="php">
<![CDATA[
<?php
// Connect to an ODBC database using an alias
$dsn = 'mydb';
$user = '';
$password = '';

try {
    $dbh = new PDO($dsn, $user, $password);
}catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>
]]>
      </programlisting>
     </example>
    </para>
   </refsect1>
  </refentry>

<!-- 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
-->