php-doc-en/reference/mysqli/mysqli/construct.xml
Joe Watkins 7a83e2a0c9 reverse the silly
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@332359 c90b9560-bf6c-de11-be94-00142212c4b1
2013-12-13 23:57:54 +00:00

298 lines
9.2 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mysqli.construct" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysqli::__construct</refname>
<refname>mysqli_connect</refname>
<refpurpose>Open a new connection to the MySQL server</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>&style.oop;</para>
<constructorsynopsis>
<methodname>mysqli::__construct</methodname>
<methodparam choice="opt"><type>string</type><parameter>host</parameter><initializer>ini_get("mysqli.default_host")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>username</parameter><initializer>ini_get("mysqli.default_user")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>passwd</parameter><initializer>ini_get("mysqli.default_pw")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>dbname</parameter><initializer>""</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>port</parameter><initializer>ini_get("mysqli.default_port")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>socket</parameter><initializer>ini_get("mysqli.default_socket")</initializer></methodparam>
</constructorsynopsis>
<para>&style.procedural;</para>
<methodsynopsis>
<type>mysqli</type><methodname>mysqli_connect</methodname>
<methodparam choice="opt"><type>string</type><parameter>host</parameter><initializer>ini_get("mysqli.default_host")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>username</parameter><initializer>ini_get("mysqli.default_user")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>passwd</parameter><initializer>ini_get("mysqli.default_pw")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>dbname</parameter><initializer>""</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>port</parameter><initializer>ini_get("mysqli.default_port")</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>socket</parameter><initializer>ini_get("mysqli.default_socket")</initializer></methodparam>
</methodsynopsis>
<para>
Opens a connection to the MySQL Server running on.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>host</parameter></term>
<listitem>
<para>
Can be either a host name or an IP address. Passing the &null; value
or the string "localhost" to this parameter, the local host is
assumed. When possible, pipes will be used instead of the TCP/IP
protocol.
</para>
<para>
Prepending host by <literal>p:</literal> opens a persistent connection.
<function>mysqli_change_user</function> is automatically called on
connections opened from the connection pool.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>username</parameter></term>
<listitem>
<para>
The MySQL user name.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>passwd</parameter></term>
<listitem>
<para>
If not provided or &null;, the MySQL server will attempt to authenticate
the user against those user records which have no password only. This
allows one username to be used with different permissions (depending
on if a password as provided or not).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>dbname</parameter></term>
<listitem>
<para>
If provided will specify the default database to be used when
performing queries.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>port</parameter></term>
<listitem>
<para>
Specifies the port number to attempt to connect to the MySQL server.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>socket</parameter></term>
<listitem>
<para>
Specifies the socket or named pipe that should be used.
</para>
<note>
<para>
Specifying the <parameter>socket</parameter> parameter will not
explicitly determine the type of connection to be used when
connecting to the MySQL server. How the connection is made to the
MySQL database is determined by the <parameter>host</parameter>
parameter.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns an object which represents the connection to a MySQL Server.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.3.0</entry>
<entry>
Added the ability of persistent connections.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><methodname>mysqli::__construct</methodname> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
?>
]]>
</programlisting>
<para>&style.oop; when extending mysqli class</para>
<programlisting role="php">
<![CDATA[
<?php
class foo_mysqli extends mysqli {
public function __construct($host, $user, $pass, $db) {
parent::__construct($host, $user, $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
}
$db = new foo_mysqli('localhost', 'my_user', 'my_password', 'my_db');
echo 'Success... ' . $db->host_info . "\n";
$db->close();
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
Success... MySQL host info: localhost via TCP/IP
]]>
</screen>
</example>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&mysqli.charset.note;
<note>
<para>
OO syntax only: If a connection fails an object is still returned. To check
if the connection failed then use either the
<function>mysqli_connect_error</function> function or the <link
linkend="mysqli.connect-error">mysqli->connect_error</link> property as in
the preceding examples.
</para>
</note>
<note>
<para>
If it is necessary to set options, such as the connection timeout,
<function>mysqli_real_connect</function> must be used instead.
</para>
</note>
<note>
<para>
Calling the constructor with no parameters is the same as calling
<function>mysqli_init</function>.
</para>
</note>
<note>
<para>
Error "Can't create TCP/IP socket (10106)" usually means that the <link
linkend="ini.variables-order">variables_order</link> configure directive
doesn't contain character <literal>E</literal>. On Windows, if the
environment is not copied the <literal>SYSTEMROOT</literal> environment
variable won't be available and PHP will have problems loading Winsock.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysqli_real_connect</function></member>
<member><function>mysqli_options</function></member>
<member><function>mysqli_connect_errno</function></member>
<member><function>mysqli_connect_error</function></member>
<member><function>mysqli_close</function></member>
</simplelist>
</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:"~/.phpdoc/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
-->