MySQL Forums
Forum List  »  Perl

fetchall_hashref odd behavior
Posted by: Mack Nathan
Date: February 16, 2006 05:16AM

I have a simple database which has a unique index on the columns week and name, so when I wanted to access it using fetchall_hashref to write to a browser I combined the two to make a key called weekname:
mysql> select concat(week,'_',name) as weekname, week,name,path,target from links where isnull(path) or isnull(target) order by week,name;
+-----------------+------------+------+--------+--------+
| weekname | week | name | path | target |
+-----------------+------------+------+--------+--------+
| 2006-03-06_mon1 | 2006-03-06 | mon1 | monday | NULL |
| 2006-03-06_mon2 | 2006-03-06 | mon2 | monday | NULL |
| 2006-03-06_mon3 | 2006-03-06 | mon3 | monday | NULL |
| 2006-03-13_mon1 | 2006-03-13 | mon1 | monday | NULL |
| 2006-03-13_mon2 | 2006-03-13 | mon2 | monday | NULL |
| 2006-03-13_mon3 | 2006-03-13 | mon3 | monday | NULL |
+-----------------+------------+------+--------+--------+

However, when I use the same select in the perl script shown below I get an extra row for each hash key (WHY?):
week=2006-03-13, name=mon2, path=monday, target=
week=, name=, path=, target=
week=2006-03-06, name=mon1, path=monday, target=
week=, name=, path=, target=
week=2006-03-06, name=mon3, path=monday, target=
week=, name=, path=, target=
week=2006-03-06, name=mon2, path=monday, target=
week=, name=, path=, target=
week=2006-03-13, name=mon3, path=monday, target=
week=, name=, path=, target=
week=2006-03-13, name=mon1, path=monday, target=
week=, name=, path=, target=

Here is the perl script fragment:
$sql_stmnt="select concat(week,'_',name) as weekname, week,name,path,target from links where isnull(path) or isnull(target) order by week,name";
my $query=$db->prepare($sql_stmnt);
$query->execute();
my $linkrecs=$query->fetchall_hashref('weekname');
foreach my $weekname(%$linkrecs)
{
print "<br>week=$linkrecs->{$weekname}->{week}, ";
print "name=$linkrecs->{$weekname}->{name}, ";
print "path=$linkrecs->{$weekname}->{path}, ";
print "target=$linkrecs->{$weekname}->{target}";
}

The only message which gets written to /var/log/httpd/error_log (many times) is one for the missing values (both the valid null values and the odd missing values) such as:
[Thu Feb 16 06:05:18 2006] [error] [client 192.168.1.103] Use of uninitialized value in concatenation (.) or string at /var/www/cgi-bin/zcafe.prl line 110., referer: http://192.168.1.103/cgi-bin/zcafe.prl?-11

(For bonus points, tell me how I can get rid of such "uninitialized value" messages? I have tried setting them to a blank value if they are missing, but the requisite if statement itself createes an "uninitialized value" message)

Options: ReplyQuote


Subject
Written By
Posted
fetchall_hashref odd behavior
February 16, 2006 05:16AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.