php-doc-en/reference/mysqli/mysqli/real-connect.xml
Jakub Vrana d316f465df Hide properties from method lists
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@322005 c90b9560-bf6c-de11-be94-00142212c4b1
2012-01-10 08:41:49 +00:00

336 lines
9.9 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mysqli.real-connect" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysqli::real_connect</refname>
<refname>mysqli_real_connect</refname>
<refpurpose>Opens a connection to a mysql server</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>&style.oop;</para>
<methodsynopsis role="oop">
<type>bool</type><methodname>mysqli::real_connect</methodname>
<methodparam choice="opt"><type>string</type><parameter>host</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>username</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>passwd</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>dbname</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>port</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>socket</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter></methodparam>
</methodsynopsis>
<para>&style.procedural;</para>
<methodsynopsis>
<type>bool</type><methodname>mysqli_real_connect</methodname>
<methodparam><type>mysqli</type><parameter>link</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>host</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>username</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>passwd</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>dbname</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>port</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>socket</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter></methodparam>
</methodsynopsis>
<para>
Establish a connection to a MySQL database engine.
</para>
<para>
This function differs from <function>mysqli_connect</function>:
</para>
<itemizedlist>
<listitem>
<para>
<function>mysqli_real_connect</function> needs a valid object which has
to be created by function <function>mysqli_init</function>.
</para>
</listitem>
<listitem>
<para>
With the <function>mysqli_options</function> function you can set various
options for connection.
</para>
</listitem>
<listitem>
<para>
There is a <parameter>flags</parameter> parameter.
</para>
</listitem>
</itemizedlist>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
&mysqli.link.description;
<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>
</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 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>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
With the parameter <parameter>flags</parameter> you can set different
connection options:
</para>
<table>
<title>Supported flags</title>
<tgroup cols="2">
<thead>
<row>
<entry>Name</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><constant>MYSQLI_CLIENT_COMPRESS</constant></entry>
<entry>Use compression protocol</entry>
</row>
<row>
<entry><constant>MYSQLI_CLIENT_FOUND_ROWS</constant></entry>
<entry>return number of matched rows, not the number of affected rows</entry>
</row>
<row>
<entry><constant>MYSQLI_CLIENT_IGNORE_SPACE</constant></entry>
<entry>Allow spaces after function names. Makes all function names reserved words.</entry>
</row>
<row>
<entry><constant>MYSQLI_CLIENT_INTERACTIVE</constant></entry>
<entry>
Allow <literal>interactive_timeout</literal> seconds (instead of
<literal>wait_timeout</literal> seconds) of inactivity before closing the connection
</entry>
</row>
<row>
<entry><constant>MYSQLI_CLIENT_SSL</constant></entry>
<entry>Use SSL (encryption)</entry>
</row>
</tbody>
</tgroup>
</table>
<note>
<para>
For security reasons the <constant>MULTI_STATEMENT</constant> flag is
not supported in PHP. If you want to execute multiple queries use the
<function>mysqli_multi_query</function> function.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.success;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><methodname>mysqli::real_connect</methodname> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init failed');
}
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
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::init();
if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!parent::real_connect($host, $user, $pass, $db)) {
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_init();
if (!$link) {
die('mysqli_init failed');
}
if (!mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!mysqli_real_connect($link, 'localhost', 'my_user', 'my_password', 'my_db')) {
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;
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysqli_connect</function></member>
<member><function>mysqli_init</function></member>
<member><function>mysqli_options</function></member>
<member><function>mysqli_ssl_set</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
-->