2016-03-23 13 views
1

現在、MySQLデータベースに接続しているPHPで書かれたコードがあります。私は、DB2への接続を変更したい、それが現在のようになります。あなたが見ることができるようにPHPでDB2に接続する(MySQLから変更する)

$this->connection = db2_connect($this->db_host, $this->username, $this->pwd); 
if(!$this->connection) { 
    $this->HandleDBError("failed to connect to database"); 
    return false; 
} 

if(!mysql_select_db($this->database, $this->connection)) { 
    $this->HandleDBError('Failed to select database '.$this->database.' See if the database name is correct '); 
    return false; 
} 

if(!mysql_query("SET NAMES 'UTF8'",$this->connection)) { 
    $this->HandleDBError('Error setting utf8 encoding'); 
    return false; 
} 

return true; 

は、私は小さな変化(db2_connect)を行いました。 DB2の場合、mysql_select_dbmysql_queryに相当するものは何ですか? (はい、私はいくつかのMySQLのステートメントが更新されていることを知っていますが、それでも動作しますか?変更が必要な場合)

答えて

0

1)db2_connect()の1番目のパラメータは$ databaseと呼ばれます。それはあなたmaysql_query()の直接対応がdb2_exec()あるmysql_select_db()

2)の同等何のヒントを与える必要がありますが、あなたは動的パラメータを持っている場合は、プリペアドステートメントを使用して検討すべきです。 db2_prepare()に関するPHPのドキュメントの例を参照:

<?php 
$animals = array(
    array(0, 'cat', 'Pook', 3.2), 
    array(1, 'dog', 'Peaches', 12.3), 
    array(2, 'horse', 'Smarty', 350.0), 
); 

$insert = 'INSERT INTO animals (id, breed, name, weight) 
    VALUES (?, ?, ?, ?)'; 
$stmt = db2_prepare($conn, $insert); 
if ($stmt) { 
    foreach ($animals as $animal) { 
     $result = db2_execute($stmt, $animal); 
    } 
} 
?> 
関連する問題