简体   繁体   English

MySQL与Firebird:使用PDO访问PHP中的查询列

[英]MySQL vs Firebird: accessing query columns in PHP with PDO

From what I understood, using PDO should be the same result regardless of what DB I am using. 据我了解,无论我使用的是哪种DB,使用PDO的结果都应该相同。 I tested this in the following code, which I have connected to two separate DB's. 我在以下代码中对此进行了测试,已将其连接到两个单独的数据库。

$sth = $pdo->query('SELECT * FROM posts');
while($result = $sth->fetch(PDO::FETCH_BOTH)){
    echo $result[1] . '<br>';
};

MySQL DB: MySQL数据库:

$dsn = 'mysql:host=' . $server . ';dbname=' . $dbname;
$pdo = new PDO($dsn, $username, $password);

Firebird DB: 火鸟数据库:

$dsn = "firebird:dbname=" . $server . ":" . $dbname;
$pdo = new PDO($dsn, $username, $password);

The MySQL connection worked fine, while the Firebird connection worked only with numbered Arrays - FETCH_NUM, and FETCH_BOTH when using index positions. 使用索引位置时,MySQL连接工作正常,而Firebird连接仅适用于编号数组-FETCH_NUM和FETCH_BOTH。 Is this how its supposed to be or am I doing something wrong with my Firebird connection? 这是应该的样子还是我的Firebird连接出现问题? I will need to work with the Firebird DB in the future, so this really frustrates me. 将来,我将需要使用Firebird数据库,因此这让我感到非常沮丧。 Thank you for all comments. 感谢您的所有评论。

In Firebird, by default, mainly from historical reasons, unless you use double quotes on object names (field names, tables, etc...), they are uppercase-d and stored internally in uppercase. 在Firebird中,默认情况下,主要是出于历史原因,除非您在对象名称(字段名称,表等)上使用双引号,否则它们是大写字母d,并在内部以大写字母存储。

Accordingly, the column names you receive in result set are in uppercase, so you should address them in upper-case like $row['FIELD_NAME'] . 因此,您在结果集中收到的列名称是大写的,因此您应使用大写形式对它们进行命名,例如$row['FIELD_NAME']

Alternatively, in PHP, PDO driver have a special flag used on connection, PDO::ATTR_CASE => PDO::CASE_LOWER/NATURAL/UPPER that adjust the needed case internally for you. 另外,在PHP中,PDO驱动程序有一个用于连接的特殊标志PDO::ATTR_CASE => PDO::CASE_LOWER/NATURAL/UPPER在内部为您调整所需的大小写。

For example: 例如:

$source = $d['kind'].':'.'dbname='.$d['host'].':'.$d['base'].';charset='.$d['charset'];
$options = $d['options'] + [
    \PDO::ATTR_CASE => \PDO::CASE_LOWER,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
    ];
$connection = new \PDO($source, $d['user'], $d['password'], $options);

Firebird is not alone doing this. Firebird并不孤单。 Oracle also stores by default metadata in uppercase. Oracle默认还以大写形式存储元数据。 Some DBMSes have options, others in lower-case by default. 一些DBMS具有选项,其他DBMS默认为小写。

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

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