<?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="mysqli">
   <modifier>public</modifier> <type>bool</type><methodname>mysqli::real_connect</methodname>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>hostname</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>username</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>password</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>database</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>port</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>socket</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
  </methodsynopsis>
  <para>&style.procedural;</para>
  <methodsynopsis>
   <type>bool</type><methodname>mysqli_real_connect</methodname>
   <methodparam><type>mysqli</type><parameter>mysql</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>hostname</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>username</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>password</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>database</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>port</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>socket</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></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>hostname</parameter></term>
     <listitem>
      <para>
       Can be either a host name or an IP address. When passing &null;, the value is retrieved from
       <link linkend="ini.mysqli.default-host">mysqli.default_host</link>.
       When possible, pipes will be used instead of the TCP/IP protocol.
       The TCP/IP protocol is used if a host name and port number are provided together e.g. <literal>localhost:3308</literal>.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>username</parameter></term>
     <listitem>
      <para>
       The MySQL username or &null; to assume the username based on the
       <link linkend="ini.mysqli.default-user">mysqli.default_user</link> ini option.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>password</parameter></term>
     <listitem>
      <para>
       The MySQL password or &null; to assume the password based on the
       <link linkend="ini.mysqli.default-pw">mysqli.default_pw</link> ini option.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>database</parameter></term>
     <listitem>
      <para>
       The default database to be used when performing queries or &null;.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>port</parameter></term>
     <listitem>
      <para>
       The port number to attempt to connect to the MySQL server or &null; to assume the port based on the
       <link linkend="ini.mysqli.default-port">mysqli.default_port</link> ini option.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>socket</parameter></term>
     <listitem>
      <para>
       The socket or named pipe that should be used or &null; to assume the socket based on the
       <link linkend="ini.mysqli.default-socket">mysqli.default_socket</link> ini option.
      </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>hostname</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 xml:id="mysqli.real-connect.flags">
       <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>
        <row>
         <entry><constant>MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT</constant></entry>
         <entry>
          Like <constant>MYSQLI_CLIENT_SSL</constant>, but disables validation of the provided 
          SSL certificate. This is only for installations using MySQL Native Driver and MySQL 5.6 or later.
         </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="errors">
  &reftitle.errors;
  &mysqli.conditionalexception;
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <para>
   <informaltable>
    <tgroup cols="2">
     <thead>
      <row>
       <entry>&Version;</entry>
       <entry>&Description;</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>7.4.0</entry>
       <entry>
        All parameters are now nullable.
       </entry>
      </row>
     </tbody>
    </tgroup>
   </informaltable>
  </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::__construct();

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