mysql_connect

(PHP 3, PHP 4, PHP 5)

mysql_connect -- MySQLサーバーへの接続をオープンする

説明

resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )

接続に成功した場合は正のMySQLリンクID、失敗した場合にFALSEを返し ます。

mysql_connect()は、MySQL サーバーへの接続を確 立します。オプションのパラメータが指定されない場合、以下のデフォ ルト値が使用されます。: server = 'localhost:3306', username = サーバプロセスの所有ユーザ名 password = 空のパスワード

パラメータserverには、ポート番号も指定する ことが可能です。例えば、"hostname:port"、または、 localhostの場合には、ソケットへのパス ":/path/to/socket"とします。

注意: Whenever you specify "localhost" or "localhost:port" 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 "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as mysql.default_host in your PHP configuration and leave the server field blank.

":port"のサポートは3.0B4で追加されました。

":/path/to/socket"のサポートは 3.0.10で追加されました。

関数名の前に@を付けることに 接続に失敗した場合のエラーメッセージを出力しないようにできます。

同じ引数で2回mysql_connect()をコールした場合、 二回目は新規のリンクが確立されるのではなく、代わりにすでにオープンされた リンクのリンクIDが返されます。 パラメータnew_linkはこの動作を変更し、既に mysql_connect()が同じパラメータでコールされてい る場合でも常に新規のリンクがオープンされるようにします。 パラメータclient_flagsは、 定数 MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE, MYSQL_CLIENT_INTERACTIVEの組み合わせとすることができます。

注意: パラメータnew_linkはPHP 4.2.0で利用可能とな りました。

パラメータclient_flagsはPHP 4.3.0で利用可能 となりました。

サーバーへのリンクは、mysql_close()のコールにより 明示的に閉じられない限り、スクリプトの実行終了と同時に閉じられます。

例 1. MySQL に接続する例

<?php
    $link
= mysql_connect("localhost", "username", "secret")
        or die(
"Could not connect");
    print (
"Connected successfully");
    
mysql_close($link);
?>

mysql_pconnect()および mysql_close()も参照ください。