Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

$dbh->errstr;

by Anonymous Monk
on Jul 24, 2002 at 13:12 UTC ( [id://184811]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

if(correctSess()) { if($xObdh) { $dbh->prepare("UPDATE items SET users='$xObdh|user=$mainuserid=$qa +t' WHERE id = '$id'") or die $dbh->errstr; } else { $dbh->prepare("UPDATE items SET users='|user=$mainuserid=$qat' WHE +RE id = '$id'") or die $dbh->errstr; } } else { if($xObdh) { $dbh->prepare("UPDATE items SET users='$xObdh|guest=$IP=$qat' WHER +E id = '$id'") or die $dbh->errstr; } else { $dbh->prepare("UPDATE items SET users='|guest=$IP=$qat' WHERE id = + '$id'") or die $dbh->errstr; } } This isnt even adding, and its not even giving an error.

Replies are listed 'Best First'.
Re: $dbh->errstr;
by Abigail-II (Bishop) on Jul 24, 2002 at 13:19 UTC
    What do you mean by "This isn't even adding"? There's no addition in your code. If you mean that no data is added to the table, two things: first, you are not doing an INSERT but an UPDATE, and second, you are never doing an execute. All you are doing is a prepare, and you discard the returned statement handle.

    Abigail

Re: $dbh->errstr;
by derby (Abbot) on Jul 24, 2002 at 13:23 UTC
    Am,

    prepare just parses the sql statement (readies it for execution). You still need to execute the sql.

    $sth = $dbh->prepare( "Your sql statement" ) or die "yadda"; $sth->execute();

    Also, you may want to look at the latest tutorial from Tim Bunce (DBI author) on how to write DBI code more idiomatically (using RaisError vice checking errstr, using prepare -vs- prepare_cached, using placeholders, etc).

    -derby

      no I just gor prepare mixd up with do, im an ediot
Re: $dbh->prepare
by flocto (Pilgrim) on Jul 24, 2002 at 13:20 UTC

    Well, your database handle ($dbh) returns a statement handle which goes out of scope pretty soon.. You might want to read about the DBI..

    -octo

Re: $dbh->errstr;
by dreadpiratepeter (Priest) on Jul 24, 2002 at 13:22 UTC
    Well for starters you need to save the statement handler returned by prepare and call it's execute method. You don't get an error because there is no error. Prepare built the statment handle fine, you just didn't use it.
    my $sth = $dbh->prepare("UPDATE items SET users='$xObdh|user=$mainuser +id=$qat' WHERE id = '$id'") or die $dbh->errstr; $sth->execute() or die $dbh->errstr;

    Secondly, you should be using placeholders. Get in the habit of using them now. It will save you effort later. With placeholders you can cache prepared statements for performance and they will always do the right thing when quoting values, making your code simpler.

    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
Re: $dbh->errstr;
by simeon2000 (Monk) on Jul 24, 2002 at 13:40 UTC
    You have to put your the return of the prepare statement somewhere. Assuming use strict;:

    my $sql = ''; if (cond) { $sql = $dbh->prepare(SQL_CODE); } elsif (cond2) { $sql = $dbh->prepare(SQL_CODE2); } else { $sql = $dbh->prepare(SQL_CODE3); } $sql->execute(@params);

    Not only are you not storing the result of the prepare, even if you did, you must predeclare your $sql recipient variable or it'll go out of scope before you can execute it!

    "Falling in love with map, one block at a time." - simeon2000

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found