繁体   English   中英

用PHP连接到DB2(从MySQL更改)

[英]Connect to DB2 (change from MySQL) in PHP

我有一个用PHP编写的代码,当前可连接到MySQL数据库。 我想更改与DB2的连接,它目前看起来像这样:

$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语句已经更新,但它仍然可以工作吗?需要做哪些更改?)

1) db2_connect( )的第一个参数称为$ database。 那应该给你一个暗示,相当于mysql_select_db()

2) maysql_query() )的直接等效项是db2_exec() ,但是如果您具有动态参数,则应考虑使用预处理语句。 请参阅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);
    }
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM