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

Re: Knowing if an Operation is Successful in MySql

by jeffa (Bishop)
on Nov 14, 2003 at 08:31 UTC ( #307040=note: print w/replies, xml ) Need Help??

in reply to Knowing if an Operation is Successful in MySql

You really should just connect with RaiseError set to true and you won't have to constantly keep checking for errors every step of the way:
my $dbh = DBI->connect( "DBI:$driver:$database:$host", $user, $pass, {RaiseError => 1}, ); my $sth = $dbh->prepare(' INSERT INTO cardbase (id,picture) VALUES (?,?) '); my $result = $sth->execute($id,$picture); # etc.
As for whether or not the row was actually inserted or not, i usually set up my database appropriately. You really should set up your cardbase table to use an auto incremented id as a primary key. That way, you don't insert the id yourself, and you won't have to worry about duplicates, which is usually the only reason why the row would not be insertable in the first place. Also note that i am not interpolating $id and $picture inside the SQL query string, but instead i am declaring them as placeholders and i pass the actual variables to execute() which will quote them for me. This is another potential spot where a query could fail.

Do yourself a big favor and read DBI recipes today. :)

UPDATE: and Use placeholders. For SECURITY! as well ...


(the triplet paradiddle with high-hat)

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-12-10 15:08 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (40 votes). Check out past polls.