M
Macaruchi
Hi!
I am a newbie using Perl and I have a big problem , for me, and it is
that I need to return a query result into a array but I cant do that.
I did this code:
sub getcodigodb()
{ local($table)= shift;
local($field)= shift;
my $code=shift;
my $dbh = open_connection();
my $sql = "SELECT * FROM $table WHERE $field=$code ";
my $sth = $dbh->prepare($sql);
$sth->execute or die "Unable to execute SQL query: $dbh->errstr
\n";
my $sth = $dbh->prepare($sql);
$sth->execute or die "Unable to execute SQL query: $dbh->errstr
\n";
$ref = $sth->fetchall_arrayref;
$sth->finish;
$dbh->disconnect;
return $ref;
}
}
$sth->fetchall_arrayref; I use this because I read that I need a perl
structure for moving into it.
This select can return 0,1 or n rows so I suppose $ref has this rows
but I dont know how to access them.
Could be this way :
@rows=&getcodigodb('mytable','myfield',3434); Is it correct?
If it is how can I access the rows. If the query doesnt find records,
what the function return to determine if the dataset exist or not?
I do this but this just print the selection I want return the
selection and manipulate whenever I want because it is in array and I
can use everything functions from array
while (@row = $sth->fetchrow_array)
{ # retrieve one row
print join(", ", @row), "\n";
}
I just want to access the selection like a cursor.
Sorry for inconveniece but I am so confused with this.
Thks in Advance!
I am a newbie using Perl and I have a big problem , for me, and it is
that I need to return a query result into a array but I cant do that.
I did this code:
sub getcodigodb()
{ local($table)= shift;
local($field)= shift;
my $code=shift;
my $dbh = open_connection();
my $sql = "SELECT * FROM $table WHERE $field=$code ";
my $sth = $dbh->prepare($sql);
$sth->execute or die "Unable to execute SQL query: $dbh->errstr
\n";
my $sth = $dbh->prepare($sql);
$sth->execute or die "Unable to execute SQL query: $dbh->errstr
\n";
$ref = $sth->fetchall_arrayref;
$sth->finish;
$dbh->disconnect;
return $ref;
}
}
$sth->fetchall_arrayref; I use this because I read that I need a perl
structure for moving into it.
This select can return 0,1 or n rows so I suppose $ref has this rows
but I dont know how to access them.
Could be this way :
@rows=&getcodigodb('mytable','myfield',3434); Is it correct?
If it is how can I access the rows. If the query doesnt find records,
what the function return to determine if the dataset exist or not?
I do this but this just print the selection I want return the
selection and manipulate whenever I want because it is in array and I
can use everything functions from array
while (@row = $sth->fetchrow_array)
{ # retrieve one row
print join(", ", @row), "\n";
}
I just want to access the selection like a cursor.
Sorry for inconveniece but I am so confused with this.
Thks in Advance!