2004-11-10 22:21:24 +00:00
|
|
|
<?xml version='1.0' encoding='iso-8859-1'?>
|
2005-09-11 06:22:26 +00:00
|
|
|
<!-- $Revision: 1.15 $ -->
|
2004-11-10 22:21:24 +00:00
|
|
|
<refentry id="function.PDO-construct">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>PDO::__construct</refname>
|
|
|
|
<refpurpose>
|
2005-01-26 05:35:40 +00:00
|
|
|
Creates a PDO instance representing a connection to a database
|
2004-11-10 22:21:24 +00:00
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
2005-01-26 14:41:31 +00:00
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2004-11-10 22:21:24 +00:00
|
|
|
<methodsynopsis>
|
2004-11-12 13:16:17 +00:00
|
|
|
<type>PDO</type><methodname>PDO::__construct</methodname>
|
2004-11-10 22:21:24 +00:00
|
|
|
<methodparam><type>string</type><parameter>dsn</parameter></methodparam>
|
2005-02-09 17:20:15 +00:00
|
|
|
<methodparam choice="opt"><type>string</type><parameter>username</parameter></methodparam>
|
|
|
|
<methodparam choice="opt"><type>string</type><parameter>password</parameter></methodparam>
|
2005-02-05 18:52:58 +00:00
|
|
|
<methodparam choice="opt"><type>array</type><parameter>driver_options</parameter></methodparam>
|
2004-11-10 22:21:24 +00:00
|
|
|
</methodsynopsis>
|
2004-11-13 22:28:28 +00:00
|
|
|
<para>
|
|
|
|
Creates a PDO instance to represent a connection to the requested
|
2005-01-26 05:35:40 +00:00
|
|
|
database.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
2004-11-13 22:28:28 +00:00
|
|
|
<variablelist>
|
2005-01-26 05:35:40 +00:00
|
|
|
<varlistentry><term>dsn</term>
|
|
|
|
<listitem>
|
2005-02-20 05:37:59 +00:00
|
|
|
<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,
|
2005-08-12 02:03:50 +00:00
|
|
|
followed by the PDO driver-specific connection syntax. Further
|
|
|
|
information is available from the <link linkend="pdo.drivers">PDO
|
|
|
|
driver-specific documentation</link>.
|
2005-02-20 05:37:59 +00:00
|
|
|
</para>
|
2005-01-26 05:35:40 +00:00
|
|
|
<para>
|
|
|
|
The <parameter>dsn</parameter> parameter supports three
|
|
|
|
different methods of specifying the arguments required to create
|
|
|
|
a database connection:
|
2005-02-20 05:37:59 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2005-01-26 05:35:40 +00:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry><term>Driver invocation</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2005-02-20 05:37:59 +00:00
|
|
|
<parameter>dsn</parameter> contains the full DSN.
|
2005-01-26 05:35:40 +00:00
|
|
|
</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>
|
2005-02-20 05:37:59 +00:00
|
|
|
<para><userinput>uri:file:///path/to/dsnfile</userinput></para>
|
2005-01-26 05:35:40 +00:00
|
|
|
</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;
|
2005-02-20 05:37:59 +00:00
|
|
|
defining the DSN string.
|
2005-01-26 05:35:40 +00:00
|
|
|
</para>
|
2005-02-20 05:47:01 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The alias must be defined in &php.ini;, and not &htaccess; or &httpd.conf;
|
|
|
|
</para>
|
|
|
|
</note>
|
2005-01-26 05:35:40 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry><term>username</term>
|
2004-11-13 22:28:28 +00:00
|
|
|
<listitem>
|
2004-11-16 20:12:45 +00:00
|
|
|
<para>
|
2005-02-09 17:20:15 +00:00
|
|
|
The user name for the DSN string. This parameter is optional for
|
|
|
|
some PDO drivers.
|
2004-11-16 20:12:45 +00:00
|
|
|
</para>
|
2004-11-13 22:28:28 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2005-01-26 05:35:40 +00:00
|
|
|
<varlistentry><term>password</term>
|
2004-11-13 22:28:28 +00:00
|
|
|
<listitem>
|
2004-11-16 20:12:45 +00:00
|
|
|
<para>
|
2005-02-09 17:20:15 +00:00
|
|
|
The password for the DSN string. This parameter is optional for
|
|
|
|
some PDO drivers.
|
2004-11-16 20:12:45 +00:00
|
|
|
</para>
|
2004-11-13 22:28:28 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2005-02-05 18:52:58 +00:00
|
|
|
<varlistentry><term>driver_options</term>
|
2004-11-13 22:28:28 +00:00
|
|
|
<listitem>
|
2004-11-16 20:12:45 +00:00
|
|
|
<para>
|
2005-02-05 18:52:58 +00:00
|
|
|
A key=>value array of driver-specific connection options.
|
2004-11-16 20:12:45 +00:00
|
|
|
</para>
|
2004-11-13 22:28:28 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
2005-01-26 05:35:40 +00:00
|
|
|
</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">
|
2004-11-13 22:28:28 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2005-08-24 11:13:06 +00:00
|
|
|
/* Connect to an ODBC database using driver invocation */
|
2004-11-13 22:28:28 +00:00
|
|
|
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
|
|
|
|
$user = 'dbuser';
|
|
|
|
$password = 'dbpass';
|
2005-02-20 05:37:59 +00:00
|
|
|
|
2004-11-13 22:28:28 +00:00
|
|
|
try {
|
2005-01-26 14:41:31 +00:00
|
|
|
$dbh = new PDO($dsn, $user, $password);
|
2005-02-20 05:37:59 +00:00
|
|
|
} catch (PDOException $e) {
|
2005-01-26 14:41:31 +00:00
|
|
|
echo 'Connection failed: ' . $e->getMessage();
|
2004-11-13 22:28:28 +00:00
|
|
|
}
|
2005-02-20 05:37:59 +00:00
|
|
|
|
2004-11-13 22:28:28 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2005-01-26 05:35:40 +00:00
|
|
|
</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>
|
2005-02-20 05:37:59 +00:00
|
|
|
<screen>
|
2004-11-13 22:28:28 +00:00
|
|
|
<![CDATA[
|
2005-02-20 05:37:59 +00:00
|
|
|
odbc:DSN=SAMPLE;UID=john;PWD=mypass
|
2004-11-13 22:28:28 +00:00
|
|
|
]]>
|
2005-02-20 05:37:59 +00:00
|
|
|
</screen>
|
2005-01-26 05:35:40 +00:00
|
|
|
<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">
|
2004-11-13 22:28:28 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2005-08-24 11:13:06 +00:00
|
|
|
/* Connect to an ODBC database using driver invocation */
|
2004-11-13 22:28:28 +00:00
|
|
|
$dsn = 'uri:file:///usr/local/dbconnect';
|
|
|
|
$user = '';
|
|
|
|
$password = '';
|
2005-02-20 05:37:59 +00:00
|
|
|
|
2004-11-13 22:28:28 +00:00
|
|
|
try {
|
2005-01-26 14:41:31 +00:00
|
|
|
$dbh = new PDO($dsn, $user, $password);
|
2005-02-20 05:37:59 +00:00
|
|
|
} catch (PDOException $e) {
|
2005-01-26 14:41:31 +00:00
|
|
|
echo 'Connection failed: ' . $e->getMessage();
|
2004-11-13 22:28:28 +00:00
|
|
|
}
|
2005-02-20 05:37:59 +00:00
|
|
|
|
2004-11-13 22:28:28 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2005-01-26 05:35:40 +00:00
|
|
|
</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>:
|
2005-02-20 05:37:59 +00:00
|
|
|
<programlisting role="ini">
|
|
|
|
[PDO]
|
|
|
|
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost"
|
|
|
|
</programlisting>
|
2005-01-26 05:35:40 +00:00
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2004-11-13 22:28:28 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2005-08-24 11:13:06 +00:00
|
|
|
/* Connect to an ODBC database using an alias */
|
2004-11-13 22:28:28 +00:00
|
|
|
$dsn = 'mydb';
|
|
|
|
$user = '';
|
|
|
|
$password = '';
|
2005-02-20 05:37:59 +00:00
|
|
|
|
2004-11-13 22:28:28 +00:00
|
|
|
try {
|
2005-01-26 14:41:31 +00:00
|
|
|
$dbh = new PDO($dsn, $user, $password);
|
2005-02-20 05:37:59 +00:00
|
|
|
}catch (PDOException $e) {
|
2005-01-26 14:41:31 +00:00
|
|
|
echo 'Connection failed: ' . $e->getMessage();
|
2004-11-13 22:28:28 +00:00
|
|
|
}
|
2005-02-20 05:37:59 +00:00
|
|
|
|
2004-11-13 22:28:28 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2005-01-26 05:35:40 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
2004-11-10 22:21:24 +00:00
|
|
|
</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
|
|
|
|
-->
|