FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » pgsql and exception
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: pgsql and exception [message #178104 is a reply to message #178100] Mon, 14 May 2012 07:45 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
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);




--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic:
Next Topic: Re: reading a newline from a properties file
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 12 20:14:44 GMT 2024

Total time taken to generate the page: 0.04513 seconds