Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 3619

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>PDO-&#62;__construct()</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="PDO Functions"
HREF="ref.pdo.html"><LINK
REL="PREVIOUS"
TITLE="PDO->commit()"
HREF="function.pdo-commit.html"><LINK
REL="NEXT"
TITLE="PDO->errorCode()"
HREF="function.pdo-errorcode.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.pdo-commit.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.pdo-errorcode.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.PDO-construct"
></A
>PDO-&#62;__construct()</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN176349"
></A
><P
>    (no version information, might be only in CVS)</P
>PDO-&#62;__construct()&nbsp;--&nbsp;
   Creates a PDO instance representing a connection to a database
  </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176352"
></A
><H2
>说明</H2
>class <B
CLASS="classname"
>PDO</B
> { <BR
></BR
>PDO <B
CLASS="methodname"
>__construct</B
> ( string dsn [, string username [, string password [, array driver_options]]] )<BR
></BR
>}<P
>&#13;   Creates a PDO instance to represent a connection to the requested
   database. 
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176373"
></A
><H2
>参数</H2
><P
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
>dsn</DT
><DD
><P
>&#13;       The Data Source Name, or DSN, contains the information required to
       connect to the database.
      </P
><P
>&#13;       In general, a DSN consists of the PDO driver name, followed by a colon,
       followed by the PDO driver-specific connection syntax. Further
       information is available from the <A
HREF="ref.pdo.html#pdo.drivers"
>PDO
       driver-specific documentation</A
>.
      </P
><P
>&#13;       The <CODE
CLASS="parameter"
>dsn</CODE
> parameter supports three
       different methods of specifying the arguments required to create
       a database connection:
      </P
><P
>&#13;       <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
>Driver invocation</DT
><DD
><P
>&#13;           <CODE
CLASS="parameter"
>dsn</CODE
> contains the full DSN.
          </P
></DD
><DT
>URI invocation</DT
><DD
><P
>&#13;           <CODE
CLASS="parameter"
>dsn</CODE
> consists of <KBD
CLASS="userinput"
>uri:</KBD
>
           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.
          </P
><P
><KBD
CLASS="userinput"
>uri:file:///path/to/dsnfile</KBD
></P
></DD
><DT
>Aliasing</DT
><DD
><P
>&#13;           <CODE
CLASS="parameter"
>dsn</CODE
> consists of a name 
           <CODE
CLASS="parameter"
>name</CODE
> that maps to 
           <TT
CLASS="literal"
>pdo.dsn.<CODE
CLASS="parameter"
>name</CODE
></TT
> in <TT
CLASS="filename"
>php.ini</TT
>
           defining the DSN string.
          </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
            The alias must be defined in <TT
CLASS="filename"
>php.ini</TT
>, and not <TT
CLASS="filename"
>.htaccess</TT
> or <TT
CLASS="filename"
>httpd.conf</TT
>
           </P
></BLOCKQUOTE
></DIV
></DD
></DL
></DIV
>
      </P
></DD
><DT
>username</DT
><DD
><P
>&#13;       The user name for the DSN string. This parameter is optional for
       some PDO drivers.
      </P
></DD
><DT
>password</DT
><DD
><P
>&#13;       The password for the DSN string. This parameter is optional for
       some PDO drivers.
      </P
></DD
><DT
>driver_options</DT
><DD
><P
>&#13;       A key=&#62;value array of driver-specific connection options.
      </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176426"
></A
><H2
>返回值</H2
><P
>&#13;   Returns a PDO object on success.
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176429"
></A
><H2
>错误/异常</H2
><P
>&#13;   <A
HREF="function.pdo-construct.html"
>PDO-&#62;__construct()</A
> throws a PDOException if the attempt
   to connect to the requested database fails.
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176433"
></A
><H2
>范例</H2
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN176436"
></A
><P
><B
>例 1. Create a PDO instance via driver invocation</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">/* Connect to an ODBC database using driver invocation */<br /></font><font color="#0000BB">$dsn </font><font color="#007700">= </font><font color="#DD0000">'mysql:dbname=testdb;host=127.0.0.1'</font><font color="#007700">;<br /></font><font color="#0000BB">$user </font><font color="#007700">= </font><font color="#DD0000">'dbuser'</font><font color="#007700">;<br /></font><font color="#0000BB">$password </font><font color="#007700">= </font><font color="#DD0000">'dbpass'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dbh </font><font color="#007700">= new </font><font color="#0000BB">PDO</font><font color="#007700">(</font><font color="#0000BB">$dsn</font><font color="#007700">, </font><font color="#0000BB">$user</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">);<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Connection failed: ' </font><font color="#007700">. </font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">getMessage</font><font color="#007700">();<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN176439"
></A
><P
><B
>例 2. Create a PDO instance via URI invocation</B
></P
><P
>&#13;     The following example assumes that the file
     <TT
CLASS="filename"
>/usr/local/dbconnect</TT
> 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:
    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>odbc:DSN=SAMPLE;UID=john;PWD=mypass</PRE
></TD
></TR
></TABLE
><P
>&#13;     The PHP script can then create a database connection by simply
     passing the <TT
CLASS="literal"
>uri:</TT
> parameter and pointing to
     the file URI:
    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">/* Connect to an ODBC database using driver invocation */<br /></font><font color="#0000BB">$dsn </font><font color="#007700">= </font><font color="#DD0000">'uri:file:///usr/local/dbconnect'</font><font color="#007700">;<br /></font><font color="#0000BB">$user </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">;<br /></font><font color="#0000BB">$password </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">;<br /><br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dbh </font><font color="#007700">= new </font><font color="#0000BB">PDO</font><font color="#007700">(</font><font color="#0000BB">$dsn</font><font color="#007700">, </font><font color="#0000BB">$user</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">);<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Connection failed: ' </font><font color="#007700">. </font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">getMessage</font><font color="#007700">();<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN176447"
></A
><P
><B
>例 3. Create a PDO instance using an alias</B
></P
><P
>&#13;     The following example assumes that <TT
CLASS="filename"
>php.ini</TT
> contains the following
     entry to enable a connection to a MySQL database using only the
     alias <TT
CLASS="literal"
>mydb</TT
>:
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="ini"
>[PDO]
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost"</PRE
></TD
></TR
></TABLE
>
    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">/* Connect to an ODBC database using an alias */<br /></font><font color="#0000BB">$dsn </font><font color="#007700">= </font><font color="#DD0000">'mydb'</font><font color="#007700">;<br /></font><font color="#0000BB">$user </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">;<br /></font><font color="#0000BB">$password </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">;<br /><br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$dbh </font><font color="#007700">= new </font><font color="#0000BB">PDO</font><font color="#007700">(</font><font color="#0000BB">$dsn</font><font color="#007700">, </font><font color="#0000BB">$user</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">);<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Connection failed: ' </font><font color="#007700">. </font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">getMessage</font><font color="#007700">();<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.pdo-commit.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.pdo-errorcode.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>PDO-&#62;commit()</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pdo.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>PDO-&#62;errorCode()</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>