2016-07-13 3 views
-1

php-interbaseのドキュメントは良好ですが、完全ではありません。特に、Firebirdを扱うための完全な例はありません。だから、どうやってやるの?FirebirdまたはInterbaseでPHPを使用する際のベストプラクティス

+1

...これは質問ですか? – jonrsharpe

+1

これを質問として言い換えて、残りの部分を自分の質問に対する回答として投稿してください。少なくとも3つの異なる点に取り組んでいるので、複数の質問に分割する方が良いかもしれません。 –

答えて

1

基本的なガイドライン

  1. はibase_connect間の選択()ibase_pconnect対() - 短い時間接続がより少ない可能競合と&バックアップを行うことができるメンテナンスが容易に活性です。データベースへの接続が処理時間の点で「高価」でない限り(大量のリアルタイム読み取り/書き込みを実行する場合)、必要に応じてibase_connect()を使用します。
  2. 常に明示的なトランザクションを使用してください。常に。簡単です - ibase_prepare()またはibase_query()のすべての呼び出しでトランザクションハンドルが必要であり、 "生の"接続ハンドルではないと仮定します。
  3. 常にibase_commit()またはibase_rollback()のいずれかを使用してトランザクションを実行してください。

読み出し動作のための基本的なテンプレート:

// These would normally come from an include file... 
$db_path = '/var/lib/firebird/2.5/data/MyDatabase.fdb'; 
$db_user = 'SYSDBA'; 
$db_pass = 'masterkey'; 

// use php error handling 
try { 
    $dbh = ibase_connect($db_path, $db_user, $db_pass); 
    // Failure to connect 
    if (!$dbh) { 
     throw new Exception('Failed to connect to database because: ' . ibase_errmsg(), ibase_errcode()); 
    } 

    $th = ibase_trans($dbh, IBASE_READ+IBASE_COMMITTED+IBASE_REC_NO_VERSION); 
    if (!$th) { 
     throw new Exception('Unable to create new transaction because: ' . ibase_errmsg(), ibase_errcode()); 
    } 

    $qs = 'select FIELD1, FIELD2, from SOMETABLE order by FIELD1'; 
    $qh = ibase_query($th, $qs); 

    if (!$qh) { 
     throw new Exception('Unable to process query because: ' . ibase_errmsg(), ibase_errcode()); 
    } 

    $rows = array(); 
    while ($row = ibase_fetch_object($qh)) { 
     $rows[] = $row->NODE; 
    } 

    // $rows[] now holds results. If there were any. 

    // Even though nothing was changed the transaction must be 
    // closed. Commit vs Rollback - question of style, but Commit 
    // is encouraged. And there shouldn't <gasp>used the S word</gasp> 
    // be an error for a read-only commit... 

    if (!ibase_commit($th)) { 
     throw new Exception('Unable to commit transaction because: ' . ibase_errmsg(), ibase_errcode()); 
    } 

    // Good form would dictate error traps for these next two... 
    // ...but these are the least likely to break... 
    // and my fingers are getting tired. 
    // Release PHP memory for the result set, and formally 
    // close the database connection. 
    ibase_free_result($qh); 
    ibase_close($dbh); 
} catch (Exception $e) { 
    echo "Caught exception: $e\n"; 
} 

// do whatever you need to do with rows[] here... 
関連する問題