为什么在 php 中找不到 WHERE 条件中使用的键作为标识符?

Why is the key used in the WHERE condition not found as an identifier in php?

我有一个 SQL 语句是这样的:

SELECT 
    ISO_id, country_name, gold, silver, bronze, total 
FROM 
    Country 
WHERE 
    ISO_id='GBR';

我用它通过 PHPPEAR 从数据库接收信息。数据库 returns GBR, UK, 29, 17, 19, 65 ,表示所有信息都正确返回。

通过PEAR调用并存储信息成功:

$countryResult =& $db->query($sql);
if(PEAR::isError($countryResult)){
    die($countryResult->getMessage());
}
$row =& $countryResult->fetchRow();

现在,如果我使用 $row['country_name']$row['gold'] 或除 $row['ISO_id'] 之外的任何其他内容,那么它将 returns 信息正确地作为普通关联数组。但是,当我使用 $row['ISO_id'] 时出现错误:

Notice: Undefined index: ISO_id in /disks/olympics/view.php on line 100 Call Stack: 0.0004 662648 1. {main}() /disks/olympics/view.php:0

我的临时解决方法是在原始 sql 语句中调用 ISO_id 两次并重命名一次,其工作方式如下:

SELECT 
    ISO_id, ISO_id as iso2, country_name, gold, silver, bronze, total 
FROM 
    Country 
WHERE 
    ISO_id='GBR';

//GBR, GBR, UK, 29, 17, 19, 65

现在我可以成功调用 $row['iso2'],因为应该已经可以作为 $row['ISO_id']

出于某种原因,在 SQL 语句的 WHERE 子句中使用原始的 ISO_id 似乎弄乱了我稍后在程序中使用它的方式。这是为什么?

如果您在 获取循环 中打印出 $row,您将看到 ISO_id 的实际密钥由 sql 服务器全部小写。因此,当您尝试将其引用为 ISO_id 而不是 iso_id 时,PHP 无法找到它。

您只需将密钥更改为“iso_id”,全部小写即可。