LXVIII. 改良版MySQL拡張サポート(mysqli)

導入

mysqli拡張サポートによってMySQL4.1以上で提供される機能を利用することが できるようになります。MySQLデータベースサーバに関する詳細は http://www.mysql.com/をご覧ください。

MySQLに関するドキュメントは http://dev.mysql.com/doc/ にあります。

このドキュメントの一部は、MySQL ABの許可を得てMySQLマニュアルから引用 したものです。

要件

これらの関数を有効にするには、mysqli拡張サポートを 有効にしてPHPをコンパイルする必要があります。

注意: mysqli拡張サポートはMySQL 4.1.2以上で動作するよう設計されています。 それ以前のバージョンについてはMySQL 拡張サポートのドキュメントをご覧ください。

インストール手順

PHPにmysqli拡張サポートを追加するには、 --with-mysqli=mysql_config_path オプションを指定してPHPをコンパイルする必要があります。 mysql_config_pathにはMySQL4.1以上に付属する mysql_configプログラムが存在するパスを 指定してください。同時に、--without-mysqlオプションを 使って標準のMySQL拡張 (これはデフォルトでコンパイルされます)をはずしてください。 mysqli拡張サポートと標準のmysql拡張を共存させる形でインストールしたい 場合には、PHPにバンドルされているlibmysqlライブラリは使用できません。 代わりにMySQL4.1未満でインストールされるクライアントライブラリを 使用してください。これにより何ら衝突させることなく MySQLでインストールされるクライアントライブラリをPHPが強制的に 使用するようにできます。

実行用の設定

これらの関数の動作は、php.iniの設定により変化します。

表 1. MySQLi 設定オプション

名前デフォルト変更の可否
mysqli.max_links"-1"PHP_INI_SYSTEM
mysqli.default_portNULLPHP_INI_ALL
mysqli.default_socketNULLPHP_INI_ALL
mysqli.default_hostNULLPHP_INI_ALL
mysqli.default_userNULLPHP_INI_ALL
mysqli.default_pwNULLPHP_INI_ALL

PHP_INI_* 定数の詳細と定義については、 設定の変更 を参照して下さい。

以下に設定ディレクティブに関す る簡単な説明を示します。

mysqli.max_links integer

持続的接続を含むプロセス毎の MySQL接続の最大数。

mysqli.default_port string

他のポートが指定されない場合、データベースサーバ接続時に 使用されるデフォルトのTCPポート番号。デフォルトが指定されない 場合、ポートは、環境変数MYSQL_TCP_PORT/etc/servicesmysql-tcpエントリ、コンパイル時の MYSQL_PORT定数の順番で取得されます。 Win32では、MYSQL_PORT定数のみが使用されます。

mysqli.default_socket string

他にソケット名が指定されない場合、ローカルな データベースサーバに接続する時のデフォルトのソケット名。

mysqli.default_host string

他のサーバ名が指定されない場合に、データベースサーバへの接続時に 使用されるデフォルトのサーバ名。 safe modeでは適用されません。

mysqli.default_user string

他のユーザ名が指定されない場合に、データベースサーバへの接続時に 使用されるデフォルトのユーザ名。 safe modeでは適用されません。

mysqli.default_password string

他のパスワードが指定されない場合に、データベースサーバへの接続時に 使用されるデフォルトのパスワード。 safe modeでは適用されません。

定義済みのクラス

mysqli

PHPとMySQLデータベースの間の接続を表します。

コンストラクタ

  • mysqli() - 新たにmysqliオブジェクトを作成します

メソッド

  • autocommit() - データベース変更時のオートコミットをオンまたはオフにします

  • change_user() - 指定したデータベース接続のユーザを変更します

  • character_set_name - データベース接続のデフォルトの文字セットを返します

  • close - オープンされている接続をクローズします

  • commit - カレントのトランザクションをコミットします

  • connect - MySQLデータベースサーバーへの新規接続をオープンします

  • debug - デバッグ処理を行います

  • dump_debug_info - デバッグ情報をダンプします

  • get_client_info - クライアントのバージョンを返します

  • get_host_info - 使用されている接続の型を返します

  • get_server_info - MySQLサーバーのバージョンを返します

  • init - mysqliオブジェクトを初期化します

  • info - 直近に実行されたクエリに関する情報を取得します

  • kill - 指定したMySQLスレッドをキルするようサーバーに指示します

  • multi_query - 複数のクエリを実行します

  • more_results - 実行した複数のクエリについて結果がまだ残ってるかどうかを確認します

  • next_result - 実行した複数のクエリから次の結果を読み込みます

  • options - オプションを設定します

  • ping - 指定したサーバー接続にpingを行い、接続がない場合には再接続します

  • prepare - SQLクエリをパースします

  • query - クエリを実行します

  • real_connect - MySQLデータベースサーバーへの接続をオープンします

  • escape_string - 接続に使用する文字セットを考慮してSQL命令が含まれる文字列の中の特殊文字をエスケープします

  • rollback - カレントのトランザクションをロールバックします

  • select_db - デフォルトのデータベースを選択します

  • set_opt - オプションを設定します

  • ssl_set - SSLパラメータを設定します

  • stat - カレントのシステムステータスを取得します

  • store_result - 直近のクエリから結果セットを伝送します

  • use_result - 直近のクエリからバッファリングされていない結果セットを伝送します

  • thread-safe - スレッドセーフかどうかを返します

プロパティ

  • affected_rows - gets the number of affected rows in a previous MySQL operation

  • errno - returns the error code for the most recent function call

  • error - returns the error string for the most recent function call

  • field_count - returns the number of columns for the most recent query

  • host_info - returns a string representing the type of connection used

  • info - retrieves information about the most recently executed query

  • insert-id - returns the auto generated id used in the last query

  • protocol_version - returns the version of the MySQL protocol used

  • sqlstate - returns a string containing the SQLSTATE error code for the last error

  • thread_id - returns the thread ID for the current connection

  • warning-count - returns the number of warnings generated during execution of the previous SQL statement

stmt

パースされた命令を表します。

メソッド

  • bind_param - Binds variables to a prepared statement

  • bind_result - Binds variables to a prepared statement for result storage

  • close - Closes a prepared statement

  • data-seek - Seeks to an arbitrary row in a statement result set

  • execute - Executes a prepared statement

  • fetch - Fetches result from a prepared statement into bound variables

  • get_metadata - Retrieves a resultset from a prepared statement for metadata information

  • send_long_data - Sends data in chunks

  • store_result - Buffers complete resultset from a prepared statement

プロパティ

  • affected_rows - Returns affected rows from last statement execution

  • field_count - Returns number of fields for a given prepared statement

  • errno - Returns errorcode for last statement function

  • errno - Returns errormessage for last statement function

  • param_count - Returns number of parameter for a given prepare statement

  • sqlstate - returns a string containing the SQLSTATE error code for the last statement function

result

データベースへのクエリにより得られた結果セットを表します。

メソッド

  • close - closes resultset

  • data_seek - moves internal result pointer

  • fetch_field - gets column information from a resultset

  • fetch_fields - gets information for all columns from a resulset

  • fetch_field_direct - gets column information for specified column

  • fetch_array - fetches a result row as an associative array, a numeric array, or both.

  • fetch_assoc - fetches a result row as an associative array

  • fetch_object - fetches a result row as an object

  • fetch_row - gets a result row as an enumerated array

  • close - frees result memory

  • field_seek - set result pointer to a specified field offset

プロパティ

定義済みの定数

表 2. MySQLi 定数

定数名説明
MYSQLI_READ_DEFAULT_GROUP (integer)  
MYSQLI_READ_DEFAULT_FILE (integer)  
MYSQLI_OPT_CONNECT_TIMEOUT (integer)  
MYSQLI_OPT_LOCAL_INFILE (integer)  
MYSQLI_INIT_COMMAND (integer)  
MYSQLI_CLIENT_SSL (integer)  
MYSQLI_CLIENT_COMPRESS (integer)  
MYSQLI_CLIENT_INTERACTIVE (integer)  
MYSQLI_CLIENT_IGNORE_SPACE (integer)  
MYSQLI_CLIENT_NO_SCHEMA (integer)  
MYSQLI_CLIENT_MULTI_QUERIES (integer)  
MYSQLI_STORE_RESULT (integer)  
MYSQLI_USE_RESULT (integer)  
MYSQLI_ASSOC (integer)  
MYSQLI_NUM (integer)  
MYSQLI_BOTH (integer)  
MYSQLI_NOT_NULL_FLAG (integer)  
MYSQLI_PRI_KEY_FLAG (integer)  
MYSQLI_UNIQUE_KEY_FLAG (integer)  
MYSQLI_MULTIPLE_KEY_FLAG (integer)  
MYSQLI_BLOB_FLAG (integer)  
MYSQLI_UNSIGNED_FLAG (integer)  
MYSQLI_ZEROFILL_FLAG (integer)  
MYSQLI_AUTO_INCREMENT_FLAG (integer)  
MYSQLI_TIMESTAMP_FLAG (integer)  
MYSQLI_SET_FLAG (integer)  
MYSQLI_NUM_FLAG (integer)  
MYSQLI_PART_KEY_FLAG (integer)  
MYSQLI_GROUP_FLAG (integer)  
MYSQLI_TYPE_DECIMAL (integer)  
MYSQLI_TYPE_TINY (integer)  
MYSQLI_TYPE_SHORT (integer)  
MYSQLI_TYPE_LONG (integer)  
MYSQLI_TYPE_FLOAD (integer)  
MYSQLI_TYPE_DOUBLE (integer)  
MYSQLI_TYPE_NULL (integer)  
MYSQLI_TYPE_TIMESTAMP (integer)  
MYSQLI_TYPE_LONGLONG (integer)  
MYSQLI_TYPE_INT24 (integer)  
MYSQLI_TYPE_DATE (integer)  
MYSQLI_TYPE_TIME (integer)  
MYSQLI_TYPE_DATETIME (integer)  
MYSQLI_TYPE_YEAR (integer)  
MYSQLI_TYPE_NEWDATE (integer)  
MYSQLI_TYPE_ENUM (integer)  
MYSQLI_TYPE_SET (integer)  
MYSQLI_TYPE_TINY_BLOB (integer)  
MYSQLI_TYPE_MEDIUM_BLOB (integer)  
MYSQLI_TYPE_LONG_BLOB (integer)  
MYSQLI_TYPE_BLOB (integer)  
MYSQLI_TYPE_VAR_STRING (integer)  
MYSQLI_TYPE_STRING (integer)  
MYSQLI_TYPE_CHAR (integer)  
MYSQLI_TYPE_INTERVAL (integer)  
MYSQLI_TYPE_GEOMETRY (integer)  
MYSQLI_BIND_STRING (integer)  
MYSQLI_BIND_INT (integer)  
MYSQLI_BIND_DOUBLE (integer)  
MYSQLI_BIND_SEND_DATA (integer)  
MYSQLI_RPL_MASTER (integer)  
MYSQLI_RPL_SLAVE (integer)  
MYSQLI_RPL_ADMIN (integer)  
MYSQLI_NEED_DATA (integer)  
MYSQLI_NO_DATA (integer)  
MYSQLI_PR_REPORT_STDERR (integer)  
MYSQLI_PR_REPORT_PORT (integer)  
MYSQLI_PR_REPORT_FILE (integer)  
目次
mysqli_affected_rows -- 直近のクエリーで変更された行の数を得る
mysqli_autocommit -- データベース更新の自動コミットをオンまたはオフにする
mysqli_bind_param -- 準備されたステートメントに値を割り当てる
mysqli_bind_result -- Alias for mysqli_stmt_bind_result()
mysqli_change_user -- 指定されたデータベース接続のユーザー名を変更する
mysqli_character_set_name -- データベース接続のデフォルトの文字コードセットを返す
mysqli_client_encoding -- Alias of mysqli_character_set_name()
mysqli_close -- データベース接続を閉じる
mysqli_commit -- 現在ののトランザクションをコミットする
mysqli_connect_errno -- Returns the error code from last connect call
mysqli_connect_error -- Returns a string description of the last connect error
mysqli_connect -- 新規にMySQLサーバへの接続をオープンする
mysqli_data_seek -- Adjusts the result pointer to an arbitary row in the result
mysqli_debug -- Performs debugging operations
mysqli_disable_reads_from_master -- 
mysqli_disable_rpl_parse -- 
mysqli_dump_debug_info -- Dump debugging information into the log
mysqli_embedded_connect -- Open a connection to an embedded mysql server.
mysqli_enable_reads_from_master -- 
mysqli_enable_rpl_parse -- 
mysqli_errno -- Returns the error code for the most recent function call
mysqli_error -- Returns a string description of the last error
mysqli_escape_string -- Alias of mysqli_real_escape_string()
mysqli_execute -- Alias for mysqli_stmt_execute()
mysqli_fetch_array -- Fetch a result row as an associative, a numeric array, or both.
mysqli_fetch_assoc -- Fetch a result row as an associative array
mysqli_fetch_field_direct --  Fetch meta-data for a single field
mysqli_fetch_field -- Returns the next field in the result set
mysqli_fetch_fields -- Returns an array of objects representing the fields in a result set
mysqli_fetch_lengths -- Returns the lengths of the columns of the current row in the result set
mysqli_fetch_object -- Returns the current row of a result set as an object
mysqli_fetch_row -- Get a result row as an enumerated array
mysqli_fetch -- Alias for mysqli_stmt_fetch()
mysqli_field_count -- Returns the number of columns for the most recent query
mysqli_field_seek --  Set result pointer to a specified field offset
mysqli_field_tell --  Get current field offset of a result pointer
mysqli_free_result -- Frees the memory associated with a result
mysqli_get_client_info -- Returns the MySQL client version as a string
mysqli_get_client_version -- Get MySQL client info.
mysqli_get_host_info -- Returns a string representing the type of connection used
mysqli_get_metadata -- Alias for mysqli_stmt_result_metadata()
mysqli_get_proto_info -- Returns the version of the MySQL protocol used
mysqli_get_server_info -- Returns the version of the MySQL server
mysqli_get_server_version -- Returns the version of the MySQL server as an integer
mysqli_info -- Retrieves information about the most recently executed query
mysqli_init --  Initializes MySQLi and returns an object for use with mysqli_real_connect
mysqli_insert_id -- Returns the auto generated id used in the last query
mysqli_kill -- Asks the server to kill a MySQL thread
mysqli_master_query -- Enforce execution of a query on the master in a master/slave setup.
mysqli_more_results -- Check if there any more query results from a multi query.
mysqli_multi_query -- Performs a query on the database
mysqli_next_result -- prepare next result from multi_query.
mysqli_num_fields --  Get the number of fields in a result
mysqli_num_rows --  Gets the number of rows in a result
mysqli_options -- set options
mysqli_param_count -- Alias for mysqli_stmt_param_count()
mysqli_ping --  Pings a server connection, or tries to reconnect if the connection has gone down.
mysqli_prepare --  Prepare a SQL statement for execution
mysqli_query -- Performs a query on the database
mysqli_real_connect -- Opens a connection to a mysql server
mysqli_real_escape_string --  Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
mysqli_real_query -- Execute an SQL query
mysqli_report -- enables or disables internal report functions
mysqli_rollback -- Rolls back current transaction
mysqli_rpl_parse_enabled -- 
mysqli_rpl_probe -- 
mysqli_rpl_query_type -- 
mysqli_select_db -- Selects the default database for database queries
mysqli_send_long_data -- Alias for mysqli_stmt_send_long_data()
mysqli_send_query -- 
mysqli_server_end -- 
mysqli_server_init -- Initialize embedded server.
mysqli_set_opt -- Alias of mysqli_options()
mysqli_sqlstate -- Returns the SQLSTATE error from previous MySQL operation.
mysqli_ssl_set -- Used for establishing secure connections using SSL.
mysqli_stat -- Gets the current system status
mysqli_stmt_affected_rows -- Returns the total number of rows changed, deleted, or inserted by the last executed statement
mysqli_stmt_bind_param -- Binds variables to a prepared statement as parameters
mysqli_stmt_bind_result -- Binds variables to a prepared statement for result storage
mysqli_stmt_close -- Closes a prepared statement
mysqli_stmt_data_seek -- Seeks to an arbitray row in statement result set
mysqli_stmt_errno -- Returns the error code for the most recent statement call
mysqli_stmt_error -- Returns a string description for last statement error
mysqli_stmt_execute -- Executes a prepared Query
mysqli_stmt_fetch --  Fetch results from a prepared statement into the bound variables
mysqli_stmt_free_result -- Frees stored result memory for the given statement handle
mysqli_stmt-init --  Initializes a statement and returns an object for use with mysqli_stmt_prepare
mysqli_stmt_num_rows -- Return the number of rows in statements result set.
mysqli_stmt_param_count -- Returns the number of parameter for the given statement
mysqli_stmt_prepare --  Prepare a SQL statement for execution
mysqli_stmt_reset -- Resets a prepared statement
mysqli_stmt_result_metadata -- returns result set metadata from a prepared statement
mysqli_stmt_send_long_data -- Send data in blocks
mysqli_stmt_sqlstate -- returns SQLSTATE error from previous statement operation
mysqli_stmt_store_result -- Transfers a result set from a prepared statement
mysqli_store_result -- Transfers a result set from the last query
mysqli_thread_id -- Returns the thread ID for the current connection
mysqli_thread_safe -- Returns whether thread safety is given or not
mysqli_use_result -- Initiate a result set retrieval
mysqli_warning_count -- Returns the number of warnings from the last query for the given link