<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.mysql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.mysql-connect.php',
    1 => 'mysql_connect',
    2 => 'Open a connection to a MySQL Server',
  ),
  'up' => 
  array (
    0 => 'ref.mysql.php',
    1 => 'MySQL Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.mysql-close.php',
    1 => 'mysql_close',
  ),
  'next' => 
  array (
    0 => 'function.mysql-create-db.php',
    1 => 'mysql_create_db',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mysql/functions/mysql-connect.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.mysql-connect" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysql_connect</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_connect</span> &mdash; <span class="dc-title">Open a connection to a MySQL Server</span></p>

 </div>

 <div id="function.mysql-connect-refsynopsisdiv">
  <div class="warning"><strong class="warning">Avviso</strong>
   <p class="para">Questa enstensione  deprecata da PHP 5.5.0, e sarà rimossa in futuro.
Al suo posto, usare l&#039;estensione <a href="book.mysqli.php" class="link">MySQLi</a> o <a href="ref.pdo-mysql.php" class="link">PDO_MySQL</a>.
Vedere anche la guida <a href="mysqlinfo.api.choosing.php" class="link">MySQL: scelta dell&#039;API</a> e
le <a href="faq.databases.php#faq.databases.mysql.deprecated" class="link">FAQ relative</a> per ulteriori informazioni. 
Le alternative a questa funzione includono:</p>
   <ul class="simplelist">
    <li><span class="function"><a href="function.mysqli-connect.php" class="function">mysqli_connect()</a></span></li>
    <li><span class="methodname"><a href="pdo.construct.php" class="methodname">PDO::__construct()</a></span></li>
   </ul>
  </div>
 </div>

 <div class="refsect1 description" id="refsect1-function.mysql-connect-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mysql_connect</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$server</code><span class="initializer"> = ini_get(&quot;mysql.default_host&quot;)</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$username</code><span class="initializer"> = ini_get(&quot;mysql.default_user&quot;)</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$password</code><span class="initializer"> = ini_get(&quot;mysql.default_password&quot;)</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$new_link</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$client_flags</code><span class="initializer"> = 0</span></span><br>): <span class="type"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">
   Opens or reuses a connection to a MySQL server.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-connect-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <dl>
   
    <dt><code class="parameter">server</code></dt>
    <dd>
     <span class="simpara">
      The MySQL server. It can also include a port number. e.g.
      &quot;hostname:port&quot; or a path to a local socket e.g. &quot;:/path/to/socket&quot; for
      the localhost.
     </span>
     <span class="simpara">
      If the PHP directive <a href="mysql.configuration.php#ini.mysql.default-host" class="link">
      mysql.default_host</a> is undefined (default), then the default
      value is &#039;localhost:3306&#039;. In <a href="ini.core.php#ini.sql.safe-mode" class="link">modalità sicura SQL</a>, this parameter is ignored
      and value &#039;localhost:3306&#039; is always used.
     </span>
    </dd>
   
   
    <dt><code class="parameter">username</code></dt>
    <dd>
     <span class="simpara">
      The username. Default value is defined by <a href="mysql.configuration.php#ini.mysql.default-user" class="link">mysql.default_user</a>. In
      <a href="ini.core.php#ini.sql.safe-mode" class="link">modalità sicura SQL</a>, this parameter is ignored and the name of the user that
      owns the server process is used.
     </span>
    </dd>
   
   
    <dt><code class="parameter">password</code></dt>
    <dd>
     <span class="simpara">
      The password. Default value is defined by <a href="mysql.configuration.php#ini.mysql.default-password" class="link">mysql.default_password</a>. In
      <a href="ini.core.php#ini.sql.safe-mode" class="link">modalità sicura SQL</a>, this parameter is ignored and empty password is used.
     </span>
    </dd>
   
   
    <dt><code class="parameter">new_link</code></dt>
    <dd>
     <span class="simpara">
      If a second call is made to <span class="function"><strong>mysql_connect()</strong></span>
      with the same arguments, no new link will be established, but
      instead, the link identifier of the already opened link will be
      returned. The <code class="parameter">new_link</code> parameter modifies this
      behavior and makes <span class="function"><strong>mysql_connect()</strong></span> always open
      a new link, even if <span class="function"><strong>mysql_connect()</strong></span> was called
      before with the same parameters.
      In <a href="ini.core.php#ini.sql.safe-mode" class="link">modalità sicura SQL</a>, this parameter is ignored.
     </span>
    </dd>
   
   
    <dt><code class="parameter">client_flags</code></dt>
    <dd>
     <span class="simpara">
      The <code class="parameter">client_flags</code> parameter can be a combination
      of the following constants:
      128 (enable <code class="literal">LOAD DATA LOCAL</code> handling),
      <strong><code><a href="mysql.constants.php#constant.mysql-client-ssl">MYSQL_CLIENT_SSL</a></code></strong>,
      <strong><code><a href="mysql.constants.php#constant.mysql-client-compress">MYSQL_CLIENT_COMPRESS</a></code></strong>,
      <strong><code><a href="mysql.constants.php#constant.mysql-client-ignore-space">MYSQL_CLIENT_IGNORE_SPACE</a></code></strong> or
      <strong><code><a href="mysql.constants.php#constant.mysql-client-interactive">MYSQL_CLIENT_INTERACTIVE</a></code></strong>.
      Read the section about <a href="mysql.constants.php#mysql.client-flags" class="xref">MySQL client constants</a> for further information.
      In <a href="ini.core.php#ini.sql.safe-mode" class="link">modalità sicura SQL</a>, this parameter is ignored.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-connect-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="simpara">
   Returns a MySQL link identifier on success o <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> in caso di fallimento.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-connect-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="function"><strong>mysql_connect()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$link</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">'Could not connect: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br />echo </span><span style="color: #DD0000">'Connected successfully'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-2">
   <p><strong>Example #2 <span class="function"><strong>mysql_connect()</strong></span> example using <code class="literal">hostname:port</code> syntax</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// we connect to example.com and port 3307<br /></span><span style="color: #0000BB">$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'example.com:3307'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$link</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">'Could not connect: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br />echo </span><span style="color: #DD0000">'Connected successfully'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// we connect to localhost at port 3307<br /></span><span style="color: #0000BB">$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'127.0.0.1:3307'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$link</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">'Could not connect: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br />echo </span><span style="color: #DD0000">'Connected successfully'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-3">
   <p><strong>Example #3 <span class="function"><strong>mysql_connect()</strong></span> example using  &quot;:/path/to/socket&quot; syntax</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// we connect to localhost and socket e.g. /tmp/mysql.sock<br /><br />// variant 1: omit localhost<br /></span><span style="color: #0000BB">$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">':/tmp/mysql'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$link</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">'Could not connect: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br />echo </span><span style="color: #DD0000">'Connected successfully'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">// variant 2: with localhost<br /></span><span style="color: #0000BB">$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost:/tmp/mysql.sock'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$link</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">'Could not connect: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br />echo </span><span style="color: #DD0000">'Connected successfully'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-connect-notes">
  <h3 class="title">Note</h3>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">
    Whenever you specify &quot;localhost&quot; or
    &quot;localhost:port&quot; as server, the MySQL client library will
    override this and try to connect to a local socket (named pipe on
    Windows). If you want to use TCP/IP, use &quot;127.0.0.1&quot;
    instead of &quot;localhost&quot;. If the MySQL client library tries to
    connect to the wrong local socket, the correct path should be set as
    <a href="mysql.configuration.php#ini.mysql.default-host" class="link">mysql.default_host</a> in <var class="filename">php.ini</var> and the server field
    left blank.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">
    The link to the server will be closed as soon as the execution of
    the script ends, unless it&#039;s closed earlier by explicitly calling
    <span class="function"><a href="function.mysql-close.php" class="function">mysql_close()</a></span>.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <span class="simpara">
    Error &quot;Can&#039;t create TCP/IP socket (10106)&quot; usually means that the <a href="ini.core.php#ini.variables-order" class="link">variables_order</a> configure directive
    doesn&#039;t contain character <code class="literal">E</code>. On Windows, if the
    environment is not copied the <code class="literal">SYSTEMROOT</code> environment
    variable won&#039;t be available and PHP will have problems loading Winsock.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-connect-seealso">
  <h3 class="title">Vedere anche:</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.mysql-pconnect.php" class="function" rel="rdfs-seeAlso">mysql_pconnect()</a> - Open a persistent connection to a MySQL server</span></li>
   <li><span class="function"><a href="function.mysql-close.php" class="function" rel="rdfs-seeAlso">mysql_close()</a> - Close MySQL connection</span></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>