首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP中连接到DB2 (从MySQL更改)

问在PHP中连接到DB2 (从MySQL更改)
EN

Stack Overflow用户
提问于 2016-03-23 19:34:20
回答 1查看 85关注 0票数 1

我有一个用PHP编写的代码,它目前连接到一个MySQL数据库。我想要更改到DB2的连接,它当前看起来如下所示:

代码语言:javascript
复制
$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_db和mysql_query的等价物是什么?(是的,我知道一些MySQL语句已经更新了,但是它仍然可以工作吗?需要做哪些更改?)

EN

回答 1

Stack Overflow用户

发布于 2016-03-23 19:54:10

1) db2_connect()的第一个参数称为$database。这应该会让您对mysql_select_db()的等价物有一点了解

2) maysql_query()的直接等价物是db2_exec(),但如果您有动态参数,则应考虑使用预准备语句。请参阅db2_prepare()上的php文档中的示例

代码语言:javascript
复制
<?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);
    }
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36177084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档