Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: MySQL count with DBI

by Itatsumaki (Friar)
on May 09, 2003 at 15:11 UTC ( [id://256900]=note: print w/replies, xml ) Need Help??


in reply to Re: MySQL count with DBI
in thread MySQL count with DBI

Not to be a pita, but I think it is vastly clearer and safer to explicitly write SQLs explicitly into a variable, rather than read them from STDIN. In addition, I'm not a huge fan of using ->fetchrow_array() to get back a single method: that is exactly what ->selectrow_array() is meant for, I think. Finally, if the questioner is just starting to use DBI he should probably enable it's error-handling apparatus explicitly until he knows when/why he might not want to use it. Similarly, I would explicitly turn off autocommit to limit accidental damage to the DB.

No major changes there, smitz, but just seems "safer" this way to me....

my $dbh = DBI->connect('DBI:mysql:my_db', 'user', 'pass', {RaiseError +=> 1, PrintError => 1, AutoCommit => 0}) or die "Error connecting to +DB: $DBI::errstr\n"; my $sql = "SELECT COUNT(*) FROM music"; my @row = $dbh->selectrow_array($sql); print "Found $row[0] results\n";
-Tats

Replies are listed 'Best First'.
Re: Re: Re: MySQL count with DBI
by smitz (Chaplain) on May 09, 2003 at 15:33 UTC
    Yeah you are right, I should have been more discriminating when I copied stephen's tut, however, Im not sure why you say:
    > it is vastly clearer and safer to explicitly write SQLs explicitly into a variable, rather than read them from STDIN.
    Where is my snippet doing that?

    Smitz

      In fact you are not: I just skimmed your code too quickly and mis-read the <<SQL as <STDIN> for some reason. My bad.

      On the other hand, I *do* think it is much better to avoid allowing an application to receive a SQL statement as a parameter. That opens up lots of scope for problems, but you probably already knew that. :)

      Update in bold. Who knew one little word could make it mean the exact opposite of what I intended? :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://256900]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-19 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found