Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6017

Re: pgsql and exception

From "M. Strobel" <sorry_no_mail_here@nowhere.dee>
Newsgroups comp.lang.php
Subject Re: pgsql and exception
Date 2012-05-14 10:18 +0200
Message-ID <a1bterF7e8U1@mid.uni-berlin.de> (permalink)
References <4fb0a891$0$1739$426a34cc@news.free.fr> <joqd6d$8bd$1@dont-email.me>

Show all headers | View raw


Am 14.05.2012 09:45, schrieb "Álvaro G. Vicario":
> El 14/05/2012 8:39, Une Bévue escribió/wrote:
>> I have an INSERT generating an exception :
>> ERROR: value too long for type character varying(30)
>>
>> this error comes from try out usinf command line.
>>
>> I'd like to print-out this error when using this database from php.
>>
>> Then, i did :
>>
>> $ret=$db->query($sql);
>> try {
>> while($row=$ret->fetch()){ // line 492
>> $rowid=$row['rowid'];
>> }
>> // do something
>> } catch (Exception $e) {
>> echo $e->getMessage()."<br />\n";
>> }
>>
>> or using :
>> } catch (PDOException $e) {
>> echo $e->getMessage()."<br />\n";
>> }
>>
>>  From php i get :
>> Fatal error: Call to a member function fetch() on a non-object in
>> /Users/yt/Sites/landp_public/landp.php on line 492
>>
>> line 492 being "while($row=$ret->fetch()){"
>>
>> obviously the best way is to avoid having a varchar column with a length
>> lower than 30.
> 
> This isn't a database error, it's a PHP error. The line where you define $ret's
> current value:
> 
> $ret=$db->query($sql);
> 
> ... failed. Such function should return a object but it didn't (probably, because it
> returned FALSE instead).
> 
> «Return Values: A query result resource on success or FALSE on failure.»
> 
> http://es.php.net/pg_query
> 
> Tips:
> 
> - Always do error checking when opening a DB connection and running a SQL query: it's
> way more likely to get an error there than when retrieving rows.
> 
> - You can inspect a variable with var_dump(), e.g.:
> 
>     var_dump($ret);
> 

It is true you have to do error checking after every step, the query/execute
functions return a PDOStatement or FALSE.

But: with PDO::ERRMODE_EXCEPTION PDO does it for you, just take care to have every DB
operation in a try block.

One extra recommendation for the readers:

you should decide on which level you write the try-catch: inside your db helper
functions, or in the top level script. The savings in code using try-catch will be
more when writing one try block in your top level script.

/Str.

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 08:39 +0200
  Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 09:10 +0200
  Re: pgsql and exception "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-14 09:34 +0200
    Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 14:17 +0200
      Re: pgsql and exception Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-14 09:38 -0400
        Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 16:59 +0200
  Re: pgsql and exception "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-05-14 09:45 +0200
    Re: pgsql and exception "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-14 10:18 +0200
    Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 14:24 +0200
      Re: pgsql and exception "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-14 14:54 +0200
        Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 15:03 +0200
      Re: pgsql and exception "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-14 14:56 +0200
        Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 15:26 +0200
          Re: pgsql and exception "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-14 22:33 +0200
          Re: pgsql and exception "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-15 09:32 +0200
        Re: pgsql and exception Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-14 09:40 -0400
          Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 17:02 +0200
            Re: pgsql and exception Shake <QUITAESTO@QUITAESTO.NOES> - 2012-05-14 17:19 +0200
              Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-14 20:46 +0200
                Re: pgsql and exception Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-14 14:55 -0400
                Re: pgsql and exception Une Bévue <unbewusstsein@fai.invalid> - 2012-05-15 08:00 +0200
                Re: pgsql and exception Luuk <luuk@invalid.lan> - 2012-05-14 21:48 +0200

csiph-web