At 2:45 PM -0500 10/26/99, Mark Russell wrote:
>That's exactly the logic of
>
>$dbh = DBI->connect($datasource, $user, $pwd);
> if ($dbh){
> $sth ...
> } else {
> &Error...
> }
>
>If the connection fails and $dbh does not exist, the }else{ &Error.. part is
>executed,
>otherwise, the process continues with $sth...
>
>My question is, under what circumstances will $dbh throw an error, and under
>what circumstances , if any, will it fail to exist. (Or more precisely, are
>there circumstances where it will simply fail to exist rather than throwing an
>error.)
It will throw an error if you enable the RaiseError attribute in the connect()
call. In this case DBI will terminate your script and connect()
will not return at all. For example:
$dbh = DBI->connect($datasource, $user, $pwd, {RaiseError => 1});
This will print a message and die if an error occurs. Then you don't need
to test the return value of $dbh. If connect() returns, it's valid.
>
>Markic
>
>Dan Ray wrote:
>
>> I believe that neither will work. When DBI->connect fails, it doesn't create
>> the $dbh object. You'll get an error when you try to call $dbh->errstr()
>> that you can't call a method on an empty object.
>>
>> Instead try:
>>
>> $dbh = DBI->connect($datasource, $user, $pwd) || &Error($DBI::errstr);
>>
>> ... You don't mention it, but I presume that &Error is a subroutine that
>> reports on errors?
>>
>> Good luck!
>>
>> Dan Ray
>> Account Manager
>> Senior Web Developer
>> Triangle Research, Inc.
>> http://www.triangleresearch.com
>>
>> > From: Mark Russell [mailto:russellm@stripped]
>> >
>> > One of my students asked the following question (for which I had no
>> > answer).
>> >
>> > If I write;
>> >
>> > ...
>> > $dbh = DBI->connect($datasource, $user, $pwd);
>> > $errstr = $dbh->errstr();
>> > if ($errstr){
>> > &Error("$errstr", @other_stuff);
>> > } else {
>> > $sth = ...
>> > }
>> > ...
>> >
>> > will any error caused by the connect be caught, or should I use instead;
>> >
>> > ...
>> > $dbh = DBI->connect($datasource, $user, $pwd);
>> > $errstr = $dbh->errstr();
>> > if ($errstr){
>> > &Error("$errstr", @other_stuff);
>> > } else {
>> > if ($dbh){
>> > $sth = ...
>> > } else {
>> > &Error("connection failed", @other_stuff);
>> > }
>> > }
>> > ...
>> >
>> >
>> >
>> > ---------------------------------------------------------------------
>> > Please check "http://www.mysql.com/Manual_chapter/manual_toc.html" before
>> > posting. To request this thread, e-mail mysql-thread16983@stripped
>> >
>> > To unsubscribe, send a message to the address shown in the
>> > List-Unsubscribe header of this message. If you cannot see it,
>> > e-mail mysql-unsubscribe@stripped instead.
>> >
>> >
>
>
>---------------------------------------------------------------------
>Please check "http://www.mysql.com/Manual_chapter/manual_toc.html" before
>posting. To request this thread, e-mail mysql-thread16987@stripped
>
>To unsubscribe, send a message to the address shown in the
>List-Unsubscribe header of this message. If you cannot see it,
>e-mail mysql-unsubscribe@stripped instead.
--
Paul DuBois, paul@stripped