downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysqli_affected_rows> <PDO_MYSQL DSN
[edit] Last updated: Mon, 01 Nov 2010

view this page in

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

導入

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

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

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

要件

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

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

インストール手順

PHP に mysqli 拡張サポートを追加するには、 --with-mysqli=mysql_config_path/mysql_config オプションを指定して PHP をコンパイルする必要があります。 mysql_config_path には MySQL 4.1 以上に付属する mysql_config プログラムが存在するパスを 指定してください。

mysqli 拡張サポートと標準の mysql 拡張を共存させる形でインストールしたい 場合には、衝突を避けるために同じクライアントライブラリを利用する必要があります。

実行時設定

php.ini の設定により動作が変化します。

表 1. MySQLi 設定オプション

名前デフォルト変更の可否変更履歴
mysqli.max_links"-1"PHP_INI_SYSTEMPHP 5.0.0 以降で使用可能です。
mysqli.default_port"3306"PHP_INI_ALLPHP 5.0.0 以降で使用可能です。
mysqli.default_socketNULLPHP_INI_ALLPHP 5.0.0 以降で使用可能です。
mysqli.default_hostNULLPHP_INI_ALLPHP 5.0.0 以降で使用可能です。
mysqli.default_userNULLPHP_INI_ALLPHP 5.0.0 以降で使用可能です。
mysqli.default_pwNULLPHP_INI_ALLPHP 5.0.0 以降で使用可能です。

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 サーバーのバージョンを返します

  • get_server_version - 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_charset - デフォルトのクライアント文字セットを設定します

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

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

  • stmt_init- mysqli_stmt_prepare で利用するステートメントを初期化します

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

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

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

プロパティ

  • affected_rows - 直近の MySQL 操作で変更された行の数を取得します

  • client_info - MySQL クライアントのバージョンを文字列で返します

  • client_version - MySQL クライアントのバージョンを整数で返します

  • errno - 直近にコールされた関数のエラーコードを返します

  • error - 直近にコールされた関数のエラー文字列を返します

  • field_count - 直近のクエリのカラム数を返します

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

  • info - 直近に実行されたクエリについての情報を取得します

  • insert_id - 直近のクエリで使用された、自動生成 ID を返します

  • protocol_version - 使用されている MySQL プロトコルのバージョンを返します

  • server_info - サーバのバージョン番号を表す文字列を返します

  • server_version - サーバのバージョン番号を整数値で返します

  • sqlstate - 直近のエラーについて、SQLSTATE エラーコードを含む文字列を返します

  • thread_id - カレントの接続のスレッド ID を返します

  • warning_count - 直近の SQL ステートメントの中で発生した警告の数を返します

mysqli_stmt

プリペアドステートメントを表します。

メソッド

  • bind_param - 変数をプリペアドステートメントにバインドします

  • bind_result - 結果を保存するため、変数をプリペアドステートメントにバインドします

  • close - プリペアドステートメントを閉じます

  • data_seek - ステートメント結果セットの任意の行に移動します

  • execute - プリペアドステートメントを実行します

  • fetch - プリペアドステートメントから結果を取得し、バインドした変数に保存します

  • free_result - 指定したステートメントハンドルの結果を保存しているメモリを開放します

  • prepare - SQL クエリを準備します

  • reset - プリペアドステートメントをリセットします

  • result_metadata - プリペアドステートメントから、メタ情報の結果セットを取得します

  • send_long_data - データを分割して送信します

  • store_result - プリペアドステートメントの結果をすべてメモリに保存します

プロパティ

  • affected_rows - 直近のステートメントの実行により更新された行数を返します

  • errno - 直近のステートメントのエラーコードを返します

  • error - 直近のステートメントのエラー文字列を返します

  • field_count - 結果セットのカラムの数を返します

  • id - ステートメントの ID を返します

  • insert_id - プリペアドステートメントで AUTO_INCREMENT のカラムに設定された値を返します

  • num_rows - 結果セットの行の数を返します

  • param_count - 指定したプリペアドステートメントのパラメータ数を返します

  • sqlstate - 直近のステートメントについて SQLSTATE エラーコードを含む文字列を返します

mysqli_result

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

メソッド

  • close - 結果セットを閉じます

  • data_seek - 内部の結果ポインタを移動します

  • fetch_array - 結果の行を連想配列・数値添字配列あるいはその両方で取得します

  • fetch_assoc - 結果の行を連想配列で取得します

  • fetch_field - 結果セットからカラムの情報を取得します

  • fetch_fields - 結果セットからすべてのカラムの情報を取得します

  • fetch_field_direct - 指定したカラムの情報を取得します

  • fetch_object - 結果の行をオブジェクトで取得します

  • fetch_row - 結果の行を数値添字の配列で取得します

  • field_seek - 結果ポインタを指定したフィールドオフセットに移動します

  • free_result - 結果のメモリを開放します

プロパティ

  • current_field - 現在のフィールドポインタのオフセットを返します

  • field_count - 結果セットのフィールド数を返します

  • lengths - カラムの長さの配列を返します

  • num_rows - 結果の行数を返します

  • type - MYSQLI_STORE_RESULT あるいは MYSQLI_USE_RESULT を返します

定義済み定数

表 2. MySQLi 定数

定数名説明
MYSQLI_READ_DEFAULT_GROUP (integer) `my.cnf' の指定した名前のグループか、あるいは MYSQLI_READ_DEFAULT_FILE で指定したファイルからオプションを読み込みます。
MYSQLI_READ_DEFAULT_FILE (integer) my.cnf のかわりに、指定したファイルからオプションを 読み込みます。
MYSQLI_OPT_CONNECT_TIMEOUT (integer) 接続のタイムアウトまでの秒数。
MYSQLI_OPT_LOCAL_INFILE (integer) LOAD LOCAL INFILE コマンドを有効にします。
MYSQLI_INIT_COMMAND (integer) MySQL サーバへの接続時に実行するコマンド。 再接続時にも自動的に再実行されます。
MYSQLI_CLIENT_SSL (integer) SSL(暗号化プロトコル)を使用します。このオプションは、アプリケーション プログラムで指定することはできず、MySQL クライアントライブラリの内部で設定します。
MYSQLI_CLIENT_COMPRESS (integer) 圧縮プロトコルを使用します。
MYSQLI_CLIENT_INTERACTIVE (integer) (wait_timeout のかわりに)interactive_timeout の秒数を使用できるように します。クライアントセッションの wait_timeout 変数の値は、interactive_timeout 変数の値に設定されます。
MYSQLI_CLIENT_IGNORE_SPACE (integer) 関数名に続く空白文字を許可します。すべての関数名を予約語とします。
MYSQLI_CLIENT_NO_SCHEMA (integer) db_name.tbl_name.col_name 形式の使用を禁止します。
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) フィールドは、NOT NULL と定義されています。
MYSQLI_PRI_KEY_FLAG (integer) フィールドは、プライマリキーの一部です。
MYSQLI_UNIQUE_KEY_FLAG (integer) フィールドは、ユニークキーの一部です。
MYSQLI_MULTIPLE_KEY_FLAG (integer) フィールドは、インデックスの一部です。
MYSQLI_BLOB_FLAG (integer) フィールドは BLOB と定義されています。
MYSQLI_UNSIGNED_FLAG (integer) フィールドは UNSIGNED と定義されています。
MYSQLI_ZEROFILL_FLAG (integer) フィールドは ZEROFILL と定義されています。
MYSQLI_AUTO_INCREMENT_FLAG (integer) フィールドは AUTO_INCREMENT と定義されています。
MYSQLI_TIMESTAMP_FLAG (integer) フィールドは TIMESTAMP と定義されています。
MYSQLI_SET_FLAG (integer) フィールドは SET と定義されています。
MYSQLI_NUM_FLAG (integer) フィールドは NUMERIC と定義されています。
MYSQLI_PART_KEY_FLAG (integer) フィールドは、マルチインデックスの一部です。
MYSQLI_GROUP_FLAG (integer) フィールドは GROUP BY の一部です。
MYSQLI_TYPE_DECIMAL (integer) フィールドは DECIMAL と定義されています。
MYSQLI_TYPE_NEWDECIMAL (integer) 精密な DECIMAL あるいは NUMERIC のフィールドです (MySQL 5.0.3 以降)。
MYSQLI_TYPE_BIT (integer) フィールドは BIT と定義されています (MySQL 5.0.3 以降)。
MYSQLI_TYPE_TINY (integer) フィールドは TINYINT と定義されています。
MYSQLI_TYPE_SHORT (integer) フィールドは INT と定義されています。
MYSQLI_TYPE_LONG (integer) フィールドは INT と定義されています。
MYSQLI_TYPE_FLOAT (integer) フィールドは FLOAT と定義されています。
MYSQLI_TYPE_DOUBLE (integer) フィールドは DOUBLE と定義されています。
MYSQLI_TYPE_NULL (integer) フィールドは DEFAULT NULL と定義されています。
MYSQLI_TYPE_TIMESTAMP (integer) フィールドは TIMESTAMP と定義されています。
MYSQLI_TYPE_LONGLONG (integer) フィールドは BIGINT と定義されています。
MYSQLI_TYPE_INT24 (integer) フィールドは MEDIUMINT と定義されています。
MYSQLI_TYPE_DATE (integer) フィールドは DATE と定義されています。
MYSQLI_TYPE_TIME (integer) フィールドは TIME と定義されています。
MYSQLI_TYPE_DATETIME (integer) フィールドは DATETIME と定義されています。
MYSQLI_TYPE_YEAR (integer) フィールドは YEAR と定義されています。
MYSQLI_TYPE_NEWDATE (integer) フィールドは DATE と定義されています。
MYSQLI_TYPE_ENUM (integer) フィールドは ENUM と定義されています。
MYSQLI_TYPE_SET (integer) フィールドは SET と定義されています。
MYSQLI_TYPE_TINY_BLOB (integer) フィールドは TINYBLOB と定義されています。
MYSQLI_TYPE_MEDIUM_BLOB (integer) フィールドは MEDIUMBLOB と定義されています。
MYSQLI_TYPE_LONG_BLOB (integer) フィールドは LONGBLOB と定義されています。
MYSQLI_TYPE_BLOB (integer) フィールドは BLOB と定義されています。
MYSQLI_TYPE_VAR_STRING (integer) フィールドは VARCHAR と定義されています。
MYSQLI_TYPE_STRING (integer) フィールドは CHAR と定義されています。
MYSQLI_TYPE_GEOMETRY (integer) フィールドは GEOMETRY と定義されています。
MYSQLI_NEED_DATA (integer) バインド変数に、さらにデータを使用可能です。
MYSQLI_NO_DATA (integer) バインド変数に使用可能なデータがありません。
MYSQLI_DATA_TRUNCATED (integer) データのトランケーションが発生しました。PHP 5.1.0 および MySQL 5.0.5 以降で使用可能です。

MySQLI ドキュメンテーションのすべての例では、MySQL AB の world データベースを使用しています。これは http://dev.mysql.com/get/Downloads/Manual/world.sql.gz/from/pick にあります。

目次
mysqli_affected_rows -- 直前の MySQL の操作で変更された行の数を得る
mysqli_autocommit -- データベース更新の自動コミットをオンまたはオフにする
mysqli_bind_param -- mysqli_stmt_bind_param() のエイリアス
mysqli_bind_result -- mysqli_stmt_bind_result() のエイリアス
mysqli_change_user -- 指定されたデータベース接続のユーザ名を変更する
mysqli_character_set_name -- データベース接続のデフォルトの文字コードセットを返す
mysqli_client_encoding -- mysqli_character_set_name() のエイリアス
mysqli_close -- 事前にオープンしているデータベース接続を閉じる
mysqli_commit -- 現在のトランザクションをコミットする
mysqli_connect_errno -- 直近の接続コールに関するエラーコードを返す
mysqli_connect_error -- 直近の接続エラーの内容を文字列で返す
mysqli_connect -- 新規に MySQL サーバへの接続をオープンする
mysqli_data_seek -- 結果の任意の行にポインタを移動する
mysqli_debug -- デバッグ操作を行う
mysqli_disable_reads_from_master -- マスタからの読み込みを無効にする
mysqli_disable_rpl_parse -- RPL のパースを無効にする
mysqli_dump_debug_info -- デバッグ情報をログに出力する
mysqli_embedded_connect -- 組み込みの mysql サーバへの接続をオープンする
mysqli_enable_reads_from_master -- マスタからの読み込みを有効にする
mysqli_enable_rpl_parse -- RPL のパースを有効にする
mysqli_errno -- 直近の関数コールによるエラーコードを返す
mysqli_error -- 直近のエラーの内容を文字列で返す
mysqli_escape_string -- mysqli_real_escape_string() のエイリアス
mysqli_execute -- mysqli_stmt_execute() のエイリアス
mysqli_fetch_array -- 結果の行を連想配列・数値添字配列あるいはその両方の形式で取得する
mysqli_fetch_assoc -- 結果の行を連想配列で取得する
mysqli_fetch_field_direct --  単一のフィールドのメタデータを取得する
mysqli_fetch_field -- 結果セットの次のフィールドを返す
mysqli_fetch_fields -- 結果セットのフィールド情報をオブジェクトの配列で返す
mysqli_fetch_lengths --  結果セットにおける現在の行のカラムの長さを返す
mysqli_fetch_object -- 結果セットの現在の行をオブジェクトとして返す
mysqli_fetch_row -- 結果の行を数値添字配列で取得する
mysqli_fetch -- mysqli_stmt_fetch() のエイリアス
mysqli_field_count -- 直近のクエリのカラムの数を返す
mysqli_field_seek --  結果ポインタを、指定したフィールドオフセットに設定する
mysqli_field_tell --  結果ポインタにおける現在のフィールドオフセットを取得する
mysqli_free_result -- 結果に関連付けられたメモリを開放する
mysqli_get_client_info -- MySQL クライアントのバージョンを文字列で返す
mysqli_get_client_version -- MySQL クライアント情報を取得する
mysqli_get_host_info -- 使用している接続の型を文字列で返す
mysqli_get_metadata -- mysqli_stmt_result_metadata() のエイリアス
mysqli_get_proto_info -- 使用している MySQL プロトコルのバージョンを返す
mysqli_get_server_info -- MySQL サーバのバージョンを返す
mysqli_get_server_version -- MySQL サーバのバージョンを整数値で返す
mysqli_info -- 直近に実行されたクエリの情報を取得する
mysqli_init --  MySQLi を初期化し、mysqli_real_connect() で使用するリソースを返す
mysqli_insert_id -- 直近のクエリで使用した自動生成の ID を返す
mysqli_kill -- サーバに MySQL スレッドの停止を問い合わせる
mysqli_master_query -- マスタ/スレーブ設定で、マスタ側のクエリを実行する
mysqli_more_results -- マルチクエリからの結果がまだ残っているかどうかを調べる
mysqli_multi_query -- データベース上でクエリを実行する
mysqli_next_result -- multi_query の、次の結果を準備する
mysqli_num_fields --  結果のフィールド数を取得する
mysqli_num_rows --  結果の行数を取得する
mysqli_options -- オプションを設定する
mysqli_param_count -- mysqli_stmt_param_count() のエイリアス
mysqli_ping --  サーバとの接続をチェックし、もし切断されている場合は再接続を試みる
mysqli_prepare --  実行するための SQL ステートメントを準備する
mysqli_query -- データベース上でクエリを実行する
mysqli_real_connect -- mysql サーバとの接続をオープンする
mysqli_real_escape_string --  接続の現在の文字セットを考慮して、SQL 文で使用する文字列の 特殊文字をエスケープする
mysqli_real_query -- SQL クエリを実行する
mysqli_report -- 内部のレポート関数を有効あるいは無効にする
mysqli_rollback -- 現在のトランザクションをロールバックする
mysqli_rpl_parse_enabled -- RPL のパースが有効かどうかを確認する
mysqli_rpl_probe -- RPL の調査
mysqli_rpl_query_type -- RPL クエリの型を返す
mysqli_select_db -- クエリを実行するためのデフォルトのデータベースを選択する
mysqli_send_long_data -- mysqli_stmt_send_long_data() のエイリアス
mysqli_send_query -- クエリを送信する
mysqli_server_end -- 組み込みのサーバをシャットダウンする
mysqli_server_init -- 組み込みのサーバを初期化する
mysqli_set_charset -- クライアントのデフォルト文字セットを設定する
mysqli_set_opt -- mysqli_options() のエイリアス
mysqli_sqlstate -- 直前の MySQL の操作での SQLSTATE エラーを返す
mysqli_ssl_set -- SSL を使用したセキュアな接続を確立する
mysqli_stat -- 現在のシステム状態を取得する
mysqli_stmt_affected_rows --  直近に実行されたステートメントで変更・削除・あるいは追加された 行の総数を返す
mysqli_stmt_bind_param -- プリペアドステートメントのパラメータに変数をバインドする
mysqli_stmt_bind_result -- 結果を保存するため、プリペアドステートメントに変数をバインドする
mysqli_stmt_close -- プリペアドステートメントを閉じる
mysqli_stmt_data_seek -- ステートメントの結果セットの任意の行に移動する
mysqli_stmt_errno -- 直近のステートメントのコールに関するエラーコードを返す
mysqli_stmt_error -- 直近のステートメントのエラー内容を文字列で返す
mysqli_stmt_execute -- プリペアドクエリを実行する
mysqli_stmt_fetch --  プリペアドステートメントから結果を取得し、バインド変数に格納する
mysqli_stmt_free_result --  指定したステートメントハンドルの結果を格納したメモリを開放する
mysqli_stmt_init --  ステートメントを初期化し、mysqli_stmt_prepare で使用する オブジェクトを返す
mysqli_stmt_num_rows -- ステートメントの結果セットの行数を返す
mysqli_stmt_param_count -- 指定したステートメントのパラメータ数を返す
mysqli_stmt_prepare --  SQL ステートメントを実行するために準備する
mysqli_stmt_reset -- プリペアドステートメントをリセットする
mysqli_stmt_result_metadata -- プリペアドステートメントから結果セットのメタデータを返す
mysqli_stmt_send_long_data -- データをブロックで送信する
mysqli_stmt_sqlstate -- 直前のステートメントの操作での SQLSTATE エラーを返す
mysqli_stmt_store_result -- プリペアドステートメントから結果を転送する
mysqli_store_result -- 直近のクエリから結果セットを転送する
mysqli_thread_id -- 現在の接続のスレッド ID を返す
mysqli_thread_safe -- スレッドセーフであるかどうかを返す
mysqli_use_result -- 結果セットの取得を開始する
mysqli_warning_count -- 指定した接続の直近のクエリから発生した警告の数を返す


mysqli_affected_rows> <PDO_MYSQL DSN
[edit] Last updated: Mon, 01 Nov 2010
 
add a note add a note User Contributed Notes MySQL 改良版拡張サポート(mysqli)
Daniel Sowden 08-Apr-2008 03:56
I had a problem compiling PHP 5.2.5 with mysqli enabled on Mac OS X. Whether this problem was unique to me I do not no, so I am posting this for anybody else that runs into the same problem. Configure would run right to the end, but I had it telling me something had failed and that it was likely messed up. Upon checking debug.log I noticed this:

dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /Users/daniel/desktop/php -5.2.5/sapi/cli/php
Reason: image not found
/bin/sh: line 1: /Users/daniel/desktop/php -5.2.5/tmp-php.ini: Permission denied
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /Users/daniel/desktop/php -5.2.5/sapi/cli/php
Reason: image not found
make: [test] Error 133 (ignored)

After screwing around for an hour and screaming many profanities at my computer I looked back at the error and noticed:

"/usr/local/mysql/lib/<b>mysql/</b>libmysqlclient.15.dylib

Why it was using this path I do not know, but I headslapped when I realised how simple it was! I solved the problem by creating a link called mysql that linked back on itself:

cd /usr/local/mysql/lib
sudo ln -s ./ mysql

Problem solved! I probably should have figured out why it was doing this instead, but this was easier and saved me from allot more frustration.
jeff at coderforlife dot com 12-Mar-2008 11:37
It looks as though mysqli persistent connections will be available by default in PHP 6.

The source code for the mysqli PHP 6 module is here in the mysql SVN:
http://svn.mysql.com/svnpublic/
php-mysqlnd/trunk/php6/ext/mysqli/mysqli.c

It has a ton a of references to persistence, and the ini setting for enabling persistence is true by default.

To bad this isn't be integrated into the latest PHP 5 builds.
swong 10-Nov-2007 07:48
If you want to extend mysqli and establish an connection with SSL authentication, the followings may be useful.
(note: not all php-mysqli supports mysqli_ssl_set())

class DataAccess extends mysqli
{
    function __construct(
          $hostname, $username, $passwd=null, $dbname=null,
          $port=null, $socket=null, $flags=null,
          $privatekey=null, $certificate=null, $caCertificate=null)
     {
           $link = parent::init();
           if ($privatekey!=null && $certificate!=null && $caCertificate!=null)
                 $link->ssl_set($privatekey, $certificate, $caCertificate,
                 dirname($caCertificate),NULL);
            parent::__construct($hostname, $username,
                 $passwd, $dbname, $port, $socket);
      }

         .....
}
marcus at synchromedia dot co dot uk 03-Oct-2007 09:27
For those having trouble getting MySQLi to work, there is another way that is set to become much more common:

http://dev.mysql.com/downloads/connector/php-mysqlnd/

This is the new MySQL native driver which has been backported from PHP6 and replaces MySQLi with a version that doesn't require any local MySQL client libraries or binaries.
smurf at smurf dot noris dot de 03-Jul-2007 05:45
Yay. No more persistent connections. I am NOT happy about that.

As soon as you get more than 250 requests per second (ever been slashdotted?) there will not be any more free TCP ports for talking to the database server, because each TCP port will be kept in TIME_WAIT for two minutes and there are only 30000 ports in the local range.

And that's not the only problem. There's also more than twice the packet rate on the network link to the database. Frankly I'd like my CPUs to spend their cycles on something producive. :-/
dan+php dot net-note-about-mysqli at obluda dot cz 27-Sep-2006 02:15
Hints for upgrading PHP code from MySQL to MySQLi:

Note - MySQLi doesn't support persistent connection. If you need it, you must implement it by self. This case is not covered in this note.

First - change all occurences of MYSQL_ to MYSQLI_ and mysql_ to mysqli_

Most of changes is required because mysqli* functions has no implicit link argument, so it need to be added explicitly if not present already. Even if it is present, most mysqli_ functions require 'link' argument as first (and mandatory) argument instead of last (and optional) as required by mysql_ functions. So, we need to change order of arguments at least.

So, you need to found names of all mysql_ functions used in your code, check if it need reoder of parameters or add the link parameter. Only *_connect() functions need review, but most scripts contain only few connect call.

If you use functions deprecated in mysql, then may not be impemented in mysqli. Those need to be reimplemented if required. In most case, it's very simple. In advance, PHP script written by carefull programer should not contain deprecated calls, so it's not problem in most cases at all.

Most of scripts I ported from mysql_ to mysqli_ can be converted by simple sed script followed by *_connect() function call review only. Especially when when programmer doesn't used the link as implicit argument and doesn't use deprecated functions the conversion of PHP source is trivial task and converted script work with no problem.

Special handling of some functions:

mysql_connect(),mysql_pconnect()
-----------------------------------------
Call with 3 or less parameters need not to be modified.
4-parameters call - delete 4th parameter as mysqli is always non-persistent
5-parameters - replace it by sequence of mysqli_init();mysqli_options();mysqli_real_connect()

mysql_create_db(), mysql_drop_db(), mysql_list_dbs(), mysql_db_name(),mysql_list_fields(), mysql_list_processes(), mysql_list_tables(), mysql_db_query(),mysql_table_name()
------------------------------------------
mysqli variant doesn't exist (those functions has been deprecated even in mysql_) but it's easy to reimplement each of it. Use apropropriate SQL command followed by standard loop for processing query results where applicable. If no connection to server yet you need to use explicit mysqli_connect call.

mysql_result()
-----------------
mysqli variant doesn't exist. Use one of mysqli_fetch_* variant (depending of type of mysql_result() third argument)

mysql_unbuffered_query()
-----------------
mysqli variant doesn't exist. Use mysqli_real_query()
chris <at> gerlt.net 07-Jul-2006 11:47
For installing mysqli extension after PHP is installed on a Linux ubuntu ( or other linux distro with apt-get such as debian, etc...):

# apt-get install php5-mysqli

NOTE: after install, you need to reboot apache
# apache2ctl graceful
Docey 08-Jun-2006 11:11
It should be noticed that mysqli does not support persistent
connections, so do not bother about implementing them
unless you got a million hits an hour on your website.

also about using both mysql and mysqli, i want to note
that under windows the best approach is to load either
mysql or mysqli at runtime not both.

i know that under php5 this is deprecated but it seems
to work fine. i used the same appoach under debian-linux
and seems to work aswell. just don't know for the
speed cost. maybe someone can benchmark this out.

just my 2 eurocents.
kronicade@yahoo 09-Mar-2006 11:42
Same problem, no solution.  I'm running Solaris sparc with mysql 4.1.12.  I've tried every config command I can think of and have modified my PATH as follows:

setenv PATH /usr/ccs/bin:
/usr/local/bin:
/usr/local/ipw/bin:
/usr/local/ipw/contrib/bin:
/sbin:/usr/sbin:
/usr/bin:
/usr/bin/X11:
/usr/ucb:
/usr/openwin/bin

./configure '--prefix=/usr/local/php' '--localstatedir=/usr/local'  '--mandir=/usr/share/man' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--includedir=/usr/lib' '--enable-shared=max'  '--enable-module=most'  '--with-imap=/usr/local/imap'  '--with-imap-ssl=/usr/local/ssl'  '--with-apxs2=/usr/local/apache2/bin/apxs'  '--enable-fastcgi'  '--enable-mbstring=all'  '--with-zlib-dir=/usr'  '--with-openssl=/usr/local/ssl'

I think it's a compatibility issue with the version of mysql running 64-bit.

-Andrew
Michael <mstclaire at myprintflow dot com> 12-Jan-2006 07:10
I was running into some random issues using the myqli extension with MySQL 5 on Mac OS X.

I discovered that when I compiled php 5 with the --with-mysqli flag, it was building php with the pre-installed MySQL 4 client libraries.

Heres how it fixed it to build php with the correct MySQL client libraries.

----------------
I had installed the binary version of MySQL 5 from MySQL AB. It installs everything in the default location /usr/local/mysql, which is fine. The MySQL version that comes with OS X ( v.4.x, depends on your OS X version ) installs the mysql_config help utility at /usr/bin/mysql_config. When php configs, it uses that one by default, inheritently using the wrong MySQL client libs.

No problem I thought, I just changed the --with-mysqli flag to --with-mysqli=/usr/local/mysql/bin/mysql_config ( or sudo find / -name mysql_config to find yours ). Nope, php throws a build error because it can't find the matching libraries. Hmmm...

So i tested /usr/local/mysql/bin/mysql_config --version, and I am shown my most current MySQL install. The problem is that the binary editions for OS X DO NOT include the shared libs ( libmysqlclient.dylib ). Oh no, I did not want to compile MySQL myself, not because I don't know how, but because MySQL AB does not recommend it, and for good reasons. Trust me, I've found out the hard way.

So what do you do? Download the source version of MySQL 5 that matches my binary version.

Configure MySQL:

./configure --enable-shared ( it's listed as ON as default, but I want to be sure )

Build MySQL:

make ( requires Developer Tools, but you knew that )

DO NOT make install !!! I repeat, DO NOT make install unless you really wish to overwrite your binary verions, which is not a good idea. ( You can configure MySQL with the --without-server flag, but I want to be certain I don't mess up )

Ok, almost done. Go to the lib directory in your MySQL build location and go to the invisible directory, .libs . There you will find your shared libraries, namely libmysqlclient.15.0.0.dylib.

Copy this to your /usr/local/mysql/lib directory. Now do the following from the lib directory:

ln -s libmysqlclient.15.0.0.dylib libmysqlclient.15.dylib
ln -s libmysqlclient.15.0.0.dylib libmysqlclient.dylib
mkdir mysql
cd mysql
ln -s ../libmysqlclient.15.0.0.dylib libmysqlclient.15.0.0.dylib
ln -s ../libmysqlclient.15.0.0.dylib libmysqlclient.15.dylib
ln -s ../libmysqlclient.15.0.0.dylib libmysqlclient.dylib

Now you can build your php with the correct library. After you build, check your phpinfo(); to validate the client version under the mysqli section. When I restarted Apache, it originally couldn't find my libraries, thus the /usr/local/mysql/lib/mysql directory.

Hope this helped.
Brad Marsh 09-Nov-2005 07:54
Notes for FreeBSD 6.0-RELEASE

MySQL 5.0.15 (source), Apache 2.0.55 (port), PHP 5.0.15 (source) - I used sources for MySQL and PHP because I could not get it all to work together using only ports.

First MySQL:
==================================
cd /usr/local/src
tar zxvf /path/to/mysql-5.0.15.tar.gz

./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data --enable-assembler --with-mysqld-ldflags=-all-static CFLAGS="-O3" CXX=gcc CXXFLAGS="-O3 -felide-constructors -fno-exceptions -fno-rtti"

make
make install

Apache:
==================================
MAKE SURE YOUR PORTS ARE UP-TO-DATE - use cvsup

cd /usr/ports/www/apache2
make
make install
make clean

PHP:
==================================
cd /usr/local/src
tar zxvf /path/to/php-5.0.5.tar.gz

./configure --with-xml --with-zlib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-apxs2=/usr/local/sbin/apxs

EXTRA STEP for mysql and mysqli extensions at the same time:
See this forum thread: http://www.kofler.cc/forum/forumthread.php?rootID=3571

In the PHP Makefile, I changed the line (note all the duplicates)

EXTRA_LIBS = -lcrypt -lcrypt -lmysqlclient -lz -lm -lxml2 -lz -liconv -lm -lxml2 -lz -liconv -lm -lmysqlclient -lz -lcrypt -lm -lxml2 -lz -liconv -lm -lcrypt -lxml2 -lz -liconv -lm -lcrypt

to

EXTRA_LIBS = -lcrypt -lmysqlclient -lz -lm -lxml2 -liconv

make
make install

Hope this helps somebody...
arjen at mysql dot com 04-Aug-2005 01:26
John Coggeshall wrote a PHP5 ext/mysqli compatibility script for applications that still use the old ext/mysql functions. This prevents the hassle of trying to have both the mysql and mysqli extensions loaded in PHP5, which can be tricky.

The script is at:
http://www.coggeshall.org/oss/mysql2i/
Andrei Nazarenko 22-Feb-2005 11:21
If you are having trouble compiling PHP5 with MySQLi support on Linux and getting a message:
"configure: error: wrong mysql library version or lib not found"
and the config.log shows an error about -lnss_files being not found, here is the solution:

edit your mysql_config file and *REMOVE* all  "-lnss_files" AND "-lnss_dns" entries from it.
After that PHP should not complain anymore.
neil 10-Feb-2005 02:15
I have spent far too much time finding this answer:

Since PHP5 many many people are having installation problems.  I found many apps were broken when I upgraded to version 5.  I tried to install mysql and mysqli and had problems.

Here's the solution:

After doing:

 ./configure --with-mysql=/path/to/mysql_config --with-mysqli=/path/to/mysql_config

do this:

"
    if you want to use both the old mysql and the new mysqli interface, load the Makefile into your editor and search for the line beginning with EXTRA_LIBS; it includes -lmysqlclient twice; remove the second instance
"

then you can:

make
make install

......
Please note:  the mysql-dev must be installed or you won't have a mysql_config anywhere.  I installed the mysql-dev rpm

Further note:  the information about the Makefile's duplicate instances of "-libmysqlclient" came from Michael Kofler.  Thanks should be directed to him.  He gives more details at the following link:

http://www.kofler.cc/forum/forumthread.php?rootID=3571
php at arwin dot net 02-Feb-2005 03:32
For those having trouble compiling with both --with-mysql AND --with-mysqli, I found a solution at this URL:

http://bugs.php.net/bug.php?id=29860&edit=1

rjanson at msn dot com writes:

OK, finally an answer!

I checked Makefile and found the following line:

EXTRA_LIBS = -lcrypt -lcrypt -lmysqlclient -lpng -lz -lz -lresolv -lm
-ldl -lnsl -lxml2 -
lz -lm -lxml2 -lz -lm -lmysqlclient -lcrypt -lnsl -lm -lz -lnss_files
-lnss_dns -lresolv -lnss_files
 -lnss_dns -lresolv -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lcrypt

By removing one of the -lmysqlclient entries on this line I was able to
successfully make and make install with both mysql and mysqli. As
confirmed by phpInfo().

I'm not sure why the other libs have multiple entries and don't cause
make to crash.
Yair Lapin 10-Nov-2004 07:28
I successed to install php support for a mysql 4.1.7 database after several attempts because the instructions are not clear.
I have a server with linux SuSe 9 enterprise with apache 1.3.31. I installed mysql from rpm files, i installed php 4.3.9 as dynamic library. this version database needs its own client library else the mysql functions in php will not work.
configuration must be as following:

./configure --with-msqli=/usr/bin/mysql_config --with-mysql=/usr  --with-apxs=/usr/local/apache/bin/apxs

 Must be installed in your machine the correct mysql librery:

MySQL-devel-4.1.7-0
MySQL-shared-4.1.7-0
nathansquires at pacbell dot net 13-Sep-2004 10:44
If you want to build php with both the Mysql and mysqli extensions, make sure your mysql distribution comes with shared libraries or build mysql yourself with shared libraries. With only static mysql libraries the compile process will fail.
severin dot kacianka at aon dot at 12-Aug-2004 03:55
I tryed Marco Kaiser's way of getting both mysql_* and mysqli_*, but it failed for me. However I could get it working this way:
First of all I installed the MySQL 4.1.3 binary disribution acourding to the manual. Then I simply passed these ./configure arguments:

--with-mysql=/path/to/mysql4.1.3
--with-mysqli=/path/to/mysql4.1.3/bin/mysql_config

This enabled both, the mysql_* and mysqli_* functions.

Severin Kacianka
phoeniks[at]nm[dot]ru 08-Apr-2004 03:21
Here is a little sample to do fast hierarchical queries using new prepared statements

<?php
// Root - lowest start element
// Top  - most top element
// stack - array for storing info

function tree_rise($root, &$stack, $top = 0) {
 
$mysqli = mysqli_connect('localhost', 'root');
 
$top  = (int)$top;
 
$stmt = mysqli_prepare($mysqli, "SELECT id, pid, title FROM news.strg WHERE id = ? LIMIT 1");
 
mysqli_stmt_bind_param($stmt, "i", $root);
 
mysqli_stmt_execute($stmt);
 
mysqli_stmt_store_result($stmt);
 
mysqli_bind_result($stmt, $id, $root, $title);

  while (
mysqli_fetch($stmt)) {
   
$stack[$id] = $title;
    if (
$root != $top && !is_null($root)) {
     
mysqli_stmt_execute($stmt);
     
mysqli_stmt_store_result($stmt);
    }
  }
  return
count($stack);
}
?>

 
show source | credits | sitemap | contact | advertising | mirror sites