为什么在WHERE条件中使用的密钥没有在php中找到作为标识符?

时间:2021-01-08 07:16:46

I have an SQL statement that goes like this:

我有一个SQL语句,如下所示:

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

And I use it to receive information from a database via PHP and PEAR. The database returns GBR, UK, 29, 17, 19, 65 , meaning that all the information was correctly returned.

我用它通过PHP和PEAR从数据库接收信息。数据库返回GBR,UK,29,17,19,65,这意味着所有信息都已正确返回。

Successfully calling and storing the information via PEAR:

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

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

Now, if I use $row['country_name'], $row['gold'], or anything else except $row['ISO_id'], then it returns the information correctly as a normal associative array. However, when I use $row['ISO_id'] I get the error:

现在,如果我使用$ row ['country_name'],$ row ['gold']或除$ row ['ISO_id']之外的任何其他内容,那么它将正确返回信息作为普通的关联数组。但是,当我使用$ 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

注意:未定义的索引:第100行的/disks/olympics/view.php中的ISO_id调用堆栈:0.0004 662648 1. {main}()/ disks/olympics/view.php:0

My temporary workaround is calling ISO_id twice in the original sql statement and renaming one, which works like so:

我的临时解决方法是在原始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

Now I can call $row['iso2'] successfully for what should already work as $row['ISO_id'].

现在我可以成功调用$ row ['iso2']作为$ row ['ISO_id']。

For some reason, it looks like using the original ISO_id in the WHERE clause in the SQL statement messes how I can use it later in the program. Why is that?

出于某种原因,看起来在SQL语句中使用WHERE子句中的原始ISO_id会混淆我以后如何在程序中使用它。这是为什么?

1 个解决方案

#1


If you print out the $row during your fetch loop you will see the actual key for the ISO_id is being returned by the sql server as all lower case. So when you try to reference it as ISO_id instead of iso_id PHP can't find it.

如果在获取循环期间打印出$ row,您将看到sql server以全小写形式返回ISO_id的实际键。因此,当您尝试将其引用为ISO_id而不是iso_id时,PHP无法找到它。

You simply need to change the key to 'iso_id' , all lowercase and you will be good to go.

你只需要将密钥更改为'iso_id',全部小写,你就可以了。

#1


If you print out the $row during your fetch loop you will see the actual key for the ISO_id is being returned by the sql server as all lower case. So when you try to reference it as ISO_id instead of iso_id PHP can't find it.

如果在获取循环期间打印出$ row,您将看到sql server以全小写形式返回ISO_id的实际键。因此,当您尝试将其引用为ISO_id而不是iso_id时,PHP无法找到它。

You simply need to change the key to 'iso_id' , all lowercase and you will be good to go.

你只需要将密钥更改为'iso_id',全部小写,你就可以了。