Issue
I have an SQL statement that goes like this:
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.
Successfully calling and storing the information via 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:
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
My temporary workaround is calling ISO_id
twice in the original sql statement and renaming one, which works like so:
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']
.
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?
Solution
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.
You simply need to change the key to 'iso_id
' , all lowercase and you will be good to go.
Answered By - Tech Savant Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.