Part1:MySQL拡張サポート(2) (1/2)

まるごと PHP!
まるごと PHP!

Part3:MySQL拡張サポート(2)

著者:桝形誠二(MASUGATA, Seiji)   2005/3/18
1   2  次のページ
プリペアドクエリとバインド変数(2)

   prepare()メソッドを実行するとクエリの解析が行われ、ステートメントオブジェクトが返されます。当然ですが、シンタックスを間違えるとエラーになるので注意しましょう。

   バインドする変数を定義する場合は、ステートメントオブジェクトのbind_param()メソッドを使います。bind_param()メソッドの1つ目の引数には、2つ目の引数以降に定義する変数の型情報を定義します。たとえば、2つ目の引数が整数、3つ目の引数が文字列であれば、1つ目の引数は「is」となります。引数の型と対応する文字は表1のとおりです。

表1:bind_param()の型情報
文字 定義
i 整数
d 浮動小数点数
s 文字列
b バイナリなどの大きなデータ

   バインドする変数を定義したら、後はステートメントオブジェクトのexecute()メソッドを使い、クエリを実行します。

   SELECT文の検索結果にバインド変数を利用することもできます。あらかじめ定義しておいた変数に、結果を代入します。検索結果を変数にバインドしたいときには、bind_result()メソッドを使って、結果を代入する変数を定義します。ステートメントオブジェクトに検索結果を取得できるメソッドが存在するので、これを利用して、定義された変数に内容を順次代入します(リスト6)。

リスト6:bindSelect.php
<?
include( "child.inc" );

try{

$ChildMySQLi = new CihildMySQLi( "localhost", "root", "", "testdb" );

$SecltStmt = $ChildMySQLi->prepare( "select no1, no2 from hoge where no1
$SecltStmt->bind_param( "i", $wNo1 );
$wNo1 = 5;
ChildStmt::execute( $ChildMySQLi,$SecltStmt );

$SecltStmt->bind_result( $No1, $No2 );

while( $SecltStmt->fetch( ) )
{
echo "--------------------\n".
"no1 = ".$No1."\n".
"no2 = ".$No2."\n\n";
}

} catch( Exception $Exception ) {

print_r( $Exception->getMessage( )."\n" );

}

?>

   プリペアドクエリとバインド変数を使った場合(リスト7)と使わない場合(リスト8)とで、どの程度の性能差が現れるかを評価してみましょう。

リスト7:bind_test.php
<?
include( "child.inc" );

try{

$ChildMySQLi = new CihildMySQLi( "localhost", "root", "", "testdb" );

$InsertStmt = $ChildMySQLi->prepare( "insert into hoge values( ?, ? )" );
$InsertStmt->bind_param( "is", $No1, $No2 );

for( $XX = 1; $XX {
$No1 = $XX;
$No2 = "test".$XX;
ChildStmt::execute( $ChildMySQLi,$InsertStmt );
}

$SecltStmt = $ChildMySQLi->prepare( "select no1, no2 from hoge order by no1" );

ChildStmt::execute( $ChildMySQLi,$SecltStmt );

$SecltStmt->bind_result( $No1,$No2 );

while( $SecltStmt->fetch( ) )
{
echo "--------------------\n".
"no1 = ".$No1."\n".
"no2 = ".$No2."\n\n";
}

} catch( Exception $Exception ) {

print_r( $Exception->getMessage( )."\n" );

}

?>

リスト8:no_bind_test.php
<?
include( "child.inc" );

try{

$ChildMySQLi = new CihildMySQLi( "localhost", "root", "", "testdb" );

for( $XX = 1; $XX {
$No1 = $XX;
$No2 = "test".$XX;

$ChildMySQLi->query( "insert into hoge values( ".$No1.", '".$No2."' )" );
}

$Result = $ChildMySQLi->query( "select no1, no2 from hoge order by no1" );

while( $Array = $Result->fetch_assoc( ) )
{
print_r( $Array );
}

} catch( Exception $Exception ) {

print_r( $Exception->getMessage( )."\n" );

}

?>
1   2  次のページ



著者プロフィール
桝形 誠二
様々な言語を経験していく中でPHPと出会い、初心者に易しいという魅力に惹かれ続けて早5年。色んな事をPHPで実装しようとしすぎて周囲の反感を買いやすいのが最近の悩み。


INDEX
Part3:MySQL拡張サポート(2)
プリペアドクエリとバインド変数(2)
  プリペアドクエリとバインド変数(3)
まるごと PHP!
Part1 PHP5のオブジェクト指向(1)
PHP5のオブジェクト指向(2)
PHP5のオブジェクト指向(3)
Part2 トライ!SQLite(1)
トライ!SQLite(2)
トライ!SQLite(3)
Part3 MySQL拡張サポート(1)
MySQL拡張サポート(2)
MySQL拡張サポート(3)
Part4 PDO(PHP Data Object)データベース抽象化レイヤクラス(1)
PDO(PHP Data Object)データベース抽象化レイヤクラス(2)
PDO(PHP Data Object)データベース抽象化レイヤクラス(3)
Part5 PHP5のXMLサポート(1)
PHP5のXMLサポート(2)
PHP5のXMLサポート(3)
Part6 PHP4からPHP5への移行のポイント

人気記事トップ10

人気記事ランキングをもっと見る