mysqli::__construct

mysqli::connect

mysqli_connect

新規に MySQL サーバーへの接続をオープンする

説明

オブジェクト指向型

public mysqli::__construct(
    stringnull $hostname = null,
    stringnull $username = null,
    stringnull $password = null,
    stringnull $database = null,
    intnull $port = null,
    stringnull $socket = null
)
public bool mysqli::connect(
    stringnull $hostname = null,
    stringnull $username = null,
    stringnull $password = null,
    stringnull $database = null,
    intnull $port = null,
    stringnull $socket = null
)

手続き型

mysqlifalse mysqli_connect(
    stringnull $hostname = null,
    stringnull $username = null,
    stringnull $password = null,
    stringnull $database = null,
    intnull $port = null,
    stringnull $socket = null
)

MySQL サーバーへの接続をオープンします。

パラメータ

hostname

ホスト名または IP アドレスです。この引数に null を渡すと mysqli.default_host から値を取得します。 可能な場合は、TCP/IP プロトコルの代わりにパイプが使用されます。 ホスト名とポートが一緒に指定された場合は、TCP/IP プロトコルが使われます。 例: localhost:3308.

ホストの前に p: をつけると、持続的な接続を開きます。 接続プールから開いた接続上で mysqli_change_user が自動的にコールされます。

username

MySQL のユーザー名。 null の場合は、mysqli.default_user iniオプションに基づいてユーザー名が決定されます。

password

MySQL のパスワード。 null の場合は、mysqli.default_pw iniオプションに基づいてパスワードが決定されます。

database

null 以外を指定した場合は、 クエリが実行されるデフォルトのデータベースとなります。

port

MySQL サーバーに接続する際のポート番号を指定します。 null の場合は、mysqli.default_port iniオプションに基づいてポート番号が決定されます。

socket

使用するソケットあるいは名前つきパイプを指定します。 null の場合は、mysqli.default_socket iniオプションに基づいてソケットが決定されます。

注意:

socket 引数を指定しても、MySQL サーバーへの 接続時の型を明示的に定義することにはなりません。MySQL サーバーへの 接続方法については hostname 引数で定義されます。

戻り値

mysqli_connect は、 MySQL サーバーへの接続を表すオブジェクトを返します。 失敗した場合に false を返します.

mysqli::connect は、成功した場合に true を返します。 失敗した場合に null を返します。

エラー / 例外

mysqli のエラー報告 (MYSQLI_REPORT_ERROR) が有効になっており、かつ要求された操作が失敗した場合は、警告が発生します。さらに、エラー報告のモードが MYSQLI_REPORT_STRICT に設定されていた場合は、mysqli_sql_exception が代わりにスローされます。

変更履歴

バージョン 説明
8.1.0 mysqli::connect は、成功時に null ではなく true を返すようになりました。
7.4.0 すべてのパラメータが、nullable になりました。

例1 mysqli::__construct の例

オブジェクト指向型

<?php

/* You should enable error reporting for mysqli before attempting to make a connection */
mysqli_report(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);

$mysqli = new mysqli('localhost''my_user''my_password''my_db');

/* Set the desired charset after establishing a connection */
$mysqli->set_charset('utf8mb4');

printf("Success... %s\n"$mysqli->host_info);

手続き型

<?php

/* You should enable error reporting for mysqli before attempting to make a connection */
mysqli_report(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);

$mysqli mysqli_connect('localhost''my_user''my_password''my_db');

/* Set the desired charset after establishing a connection */
mysqli_set_charset($mysqli'utf8mb4');

printf("Success... %s\n"mysqli_get_host_info($mysqli));

上の例の出力は、 たとえば以下のようになります。

Success... localhost via TCP/IP

例2 mysqli クラスを拡張する

<?php

class FooMysqli extends mysqli {
    public function 
__construct($host$user$pass$db$port$socket$charset) {
        
mysqli_report(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
        
parent::__construct($host$user$pass$db$port$socket);
        
$this->set_charset($charset);
    }
}

$db = new FooMysqli('localhost''my_user''my_password''my_db'3306null'utf8mb4');

例3 手動によるエラーハンドリング

エラーの報告機能が無効になっている場合、 開発者がエラーのチェックとエラー処理を行う必要があります。

オブジェクト指向型

<?php

error_reporting
(0);
mysqli_report(MYSQLI_REPORT_OFF);
$mysqli = new mysqli('localhost''my_user''my_password''my_db');
if (
$mysqli->connect_errno) {
    throw new 
RuntimeException('mysqli connection error: ' $mysqli->connect_error);
}

/* Set the desired charset after establishing a connection */
$mysqli->set_charset('utf8mb4');
if (
$mysqli->errno) {
    throw new 
RuntimeException('mysqli error: ' $mysqli->error);
}

手続き型

<?php

error_reporting
(0);
mysqli_report(MYSQLI_REPORT_OFF);
$mysqli mysqli_connect('localhost''my_user''my_password''my_db');
if (
mysqli_connect_errno()) {
    throw new 
RuntimeException('mysqli connection error: ' mysqli_connect_error());
}

/* Set the desired charset after establishing a connection */
mysqli_set_charset($mysqli'utf8mb4');
if (
mysqli_errno($mysqli)) {
    throw new 
RuntimeException('mysqli error: ' mysqli_error($mysqli));
}

注意

注意:

MySQLnd は常に、サーバーのデフォルト文字セットを想定しています。この文字セットは接続時の ハンドシェイク/認証 のときに送信され、これを mysqlnd が使います。

Libmysqlclient が使うデフォルトの文字セットは my.cnf で設定したものです。あるいは明示的に mysqli_options をコールして設定することもできます。 これは、mysqli_init のあとで mysqli_real_connect を実行する前にコールします。

注意:

オブジェクト指向型 に関する注意: 接続に失敗した場合にもオブジェクトが返されます。 接続が失敗したかどうかを確かめるには、先ほどの例のように mysqli_connect_error 関数あるいは mysqli->connect_error プロパティを使用しましょう。

注意:

接続タイムアウトなどのオプションを設定する必要がある場合は、かわりに mysqli_real_connect を使わなければなりません。

注意:

コンストラクタをパラメータなしでコールするのは、 mysqli_init をコールするのと同じ意味になります。

注意:

エラー "Can't create TCP/IP socket (10106)" が発生するのは、たいていは variables_order 設定ディレクティブに E が含まれていない場合です。 Windows では、これが含まれていなければ SYSTEMROOT 環境変数が使用できず、PHP が Winsock の読み込みに失敗します。

参考

  • mysqli_real_connect
  • mysqli_options
  • mysqli_connect_errno
  • mysqli_connect_error
  • mysqli_close