Hi all,
I have what seems to me to be a strange problem....
I have the script below:
<code>
if ($type eq 'alpha') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where name like '$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
} elsif ($type eq '') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where keywords like '%$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
}
my $tbl_ref = $dbh->selectall_arrayref ($query);
$dbh->disconnect ( );
# Display results as HTML table
for (my $i = 0; $i < $per_page && $i < @{$tbl_ref}-1; $i+=2) {
# get data values in row $i
my @cells = @{$tbl_ref->[$i]}; # get data values in row $i
my @cells2 = @{$tbl_ref->[$i+1]}; # get data values in row $i+1
# map values to HTML-encoded values, or to if null/empty
@cells = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells;
@cells2 = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells2;
# add cells to table
my @cells = "<b>$cells[0]</b><br>$cells[1]<br>$cells[2]<br>
$cells[3]<br>";
my @cells2 = "<b>$cells2[0]</b><br>$cells2[1]<br>$cells2[2]<br>
$cells2[3]<br>";
push (@rows, Tr (td (\@cells),(td (\@cells2))));
}
</code>
It works great. However if I change it to:
<code>
if ($search = '#') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley where REGEXP '^[0-9]'
ORDER BY name LIMIT %d,%d",
$start - 1,
$per_page + 1);
} elsif ($type eq 'alpha') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where name like '$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
} elsif ($type eq '') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where keywords like '%$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
}
my $tbl_ref = $dbh->selectall_arrayref ($query);
$dbh->disconnect ( );
# Line 139 is the FOR below......
for (my $i = 0; $i < $per_page && $i < @{$tbl_ref}-1; $i+=2) {
# get data values in row $i
my @cells = @{$tbl_ref->[$i]}; # get data values in row $i
my @cells2 = @{$tbl_ref->[$i+1]}; # get data values in row $i+1
# map values to HTML-encoded values, or to if null/empty
@cells = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells;
@cells2 = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells2;
# add cells to table
my @cells = "<b>$cells[0]</b><br>$cells[1]<br>$cells[2]<br>
$cells[3]<br>";
my @cells2 = "<b>$cells2[0]</b><br>$cells2[1]<br>$cells2[2]<br>
$cells2[3]<br>";
push (@rows, Tr (td (\@cells),(td (\@cells2))));
}
</code>
I get the error 'Can't use an undefined value as an ARRAY reference at search.cgi line 139.'
My questions, why would adding that code at the beginning cause that error and how can I fix it?
Any ideas/suggestions greatly appreciated.
Jim
I have what seems to me to be a strange problem....
I have the script below:
<code>
if ($type eq 'alpha') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where name like '$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
} elsif ($type eq '') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where keywords like '%$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
}
my $tbl_ref = $dbh->selectall_arrayref ($query);
$dbh->disconnect ( );
# Display results as HTML table
for (my $i = 0; $i < $per_page && $i < @{$tbl_ref}-1; $i+=2) {
# get data values in row $i
my @cells = @{$tbl_ref->[$i]}; # get data values in row $i
my @cells2 = @{$tbl_ref->[$i+1]}; # get data values in row $i+1
# map values to HTML-encoded values, or to if null/empty
@cells = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells;
@cells2 = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells2;
# add cells to table
my @cells = "<b>$cells[0]</b><br>$cells[1]<br>$cells[2]<br>
$cells[3]<br>";
my @cells2 = "<b>$cells2[0]</b><br>$cells2[1]<br>$cells2[2]<br>
$cells2[3]<br>";
push (@rows, Tr (td (\@cells),(td (\@cells2))));
}
</code>
It works great. However if I change it to:
<code>
if ($search = '#') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley where REGEXP '^[0-9]'
ORDER BY name LIMIT %d,%d",
$start - 1,
$per_page + 1);
} elsif ($type eq 'alpha') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where name like '$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
} elsif ($type eq '') {
$query = sprintf (
"SELECT name, address, city, phone
FROM valley
where keywords like '%$search%'
ORDER BY name LIMIT %d,%d",
$start - 1, # number of records to skip
$per_page + 1); # number of records to select
}
my $tbl_ref = $dbh->selectall_arrayref ($query);
$dbh->disconnect ( );
# Line 139 is the FOR below......
for (my $i = 0; $i < $per_page && $i < @{$tbl_ref}-1; $i+=2) {
# get data values in row $i
my @cells = @{$tbl_ref->[$i]}; # get data values in row $i
my @cells2 = @{$tbl_ref->[$i+1]}; # get data values in row $i+1
# map values to HTML-encoded values, or to if null/empty
@cells = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells;
@cells2 = map {
defined ($_) && $_ ne "" ? escapeHTML ($_) : " "
} @cells2;
# add cells to table
my @cells = "<b>$cells[0]</b><br>$cells[1]<br>$cells[2]<br>
$cells[3]<br>";
my @cells2 = "<b>$cells2[0]</b><br>$cells2[1]<br>$cells2[2]<br>
$cells2[3]<br>";
push (@rows, Tr (td (\@cells),(td (\@cells2))));
}
</code>
I get the error 'Can't use an undefined value as an ARRAY reference at search.cgi line 139.'
My questions, why would adding that code at the beginning cause that error and how can I fix it?
Any ideas/suggestions greatly appreciated.
Jim