> Hello,
> 
> i'm trying to make a class to do select/insert/delete/update on a
MySQL table (via DBI).
> this is a snip of code:
>

You haven't really shown us a class, just a method...
 
> sub query{
>         my $self = shift;
>         my($sql) = @_;
>         my @result;

Why scope @result here?

>         my $sth = $self->{dbh}->prepare($sql) or return undef;

Always returning undef on an error is kind of blinding, I like to know
where things are getting messed up.

>         if($sql =~ /delete|insert/gi){

What happens if 'delete' or 'insert' are in the query string?  as say
part of a text block?

>                 $sth->execute() or return undef;
>                 my $rows = $sth->rows;
>                 ($rows == 0) ? "0E0" : $rows;
>         } else {
>                 $sth->execute() or return undef;
>                 my @row;
>                 while(@row = $sth->fetchrow_array){
>                         foreach my $i (0..$#row){
>                                 push(@result,$row[$i]);
>                         }

I most often prefer to use a hash reference so that I can index into my
result set using field names rather than indexes. Using this method
there is no way to access the results via name, which means I have to
update my code every time a field is added *especially* when it is not
added on the end of a table.

>                 }
>                 $sth->finish();
>                 return @result;
>         }
> }
> 
> I like to receive an opnion or someone to indicate me something similars.
> 

You might want to look into the Class::DBI module from CPAN.

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to