pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Papp Gyozo <pgerzson@freestart.hu>
To: test test <dcmkx10@yahoo.com>
To: pgsql-php@postgresql.org
Subject: Re: Inserting Null Value or String Value
Date: Tue, 1 Oct 2002 20:41:57 +0200
Message-ID: <E17wSFC-0003rO-00@hapcibeno.freestart.hu> (raw)
In-Reply-To: <20021001165453.882.qmail@web14410.mail.yahoo.com>
References: <20021001165453.882.qmail@web14410.mail.yahoo.com>

Hi Sean, 

see below!

2002. október 1. 18:54 dátummal test test ezt írta:
| Hi,
|
| I am trying to insert a date into a date field, but also sometimes I
| need to insert a null value.  Inserting the null value seems to
| require not using quotes around null, but if I try to enter a date
| without quotes around it, it thinks it is a number.
|
| Here is the error I get: Warning: PostgreSQL query failed: ERROR:
| column "spec_start_date" is of type 'date' but expression is of type
| 'integer' You will need to rewrite or cast the expression
|
| The value I am sending is: spec_start_date = 10/01/02
|
| If I surround the date with single quotes it should be fine, but I
| can't do that when I want to insert a null value.  I know that I can
| do an "IF <use quotes> ELSE <don't use quotes>" in the SQL string, by
| I am entering many values and this would really get messy. I was
| hoping that there was a better solution. Thanks for any help.
|
| Sean
|

Beware crude hack is coming!!!

I wrote a fairly plain function which does the dirty quoting to any 
kind of values to be inserted. It looks like:


function quote($var, $type, $empty_null = TRUE)
{
  if ( empty($var) && $empty_null ){
    return 'NULL';
  }
  // if type is prepended with _ it means "array of ..."
  if ( $type{0} == '_' ){
    $type = substr($type, 1);
    $temp = "'{";
    foreach ( $var as $item ){
      $temp .= quote($item, $type).','; // recursive call
    }
    $temp = substr($temp, 0, -1);
    return $temp ."}'";
  }
  switch ( $type ){
  case 'int':
    return intval($var);
  case 'bool':
    return ($var ? "'t'::bool": "'f'::bool");
  case 'date':
    if ( is_numeric($var) ) { // treats as timestamp integer
      return date("'Y-m-d'", $var);
    }
  // otherwise as a date string
  case 'text':
    if (!get_magic_quotes_gpc()) {
      $var = addslashes($var);
    }
    return '\''.$var.'\'';
  default: trigger_error("unknown type : $type ($value)", E_USER_ERROR);
  }
}

You must notice that $var holds the corresponding value to be inserted 
or not set (in this case the function returns with NULL), $type is 
neither a legal type from PHP nor from Postgresql, but I think you can 
figure out how it works.

In my app, I have a PHP array which keeps information about what fields 
needs to be inserted and what type these fields have.


$PROPERTIES = array (
'seatnum'     => 'int',
'myear'       => 'int',
'kmage'       => 'int',
'price'       => 'int',
'volume'      => 'int',
'power'       => 'int',
'pprpm'       => 'int',
'color'       => 'text',
'damaged'     => 'bool',
'condition'   => 'int',
'ownernum'    => 'int',
'regvalid'    => 'text',
'cylindernum' => 'int',
'cylinderarr' => 'int',
'gearnum'     => 'int',
'gearing'     => 'int',
'autoclutch'  => 'bool',
'truss'       => 'int',
'trussmaterial' => 'int',
'frontbreak'    => 'int',
'rarebreak'   => 'int',
'accessories' => '_int',
'notes'       => 'text'
);

finally I have to loop through this array  and simply calls quote to 
quote the posted variables.

hth,
-- 
Papp, Győző
- pgerzson@freestart.hu



view thread (8+ messages)  latest in thread

Message-ID: <E17wSFC-0003rO-00@hapcibeno.freestart.hu>
Permalink:  ../E17wSFC-0003rO-00@hapcibeno.freestart.hu/
Also on:    postgresql.org/message-id/E17wSFC-0003rO-00@hapcibeno.freestart.hu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: pgerzson@freestart.hu, dcmkx10@yahoo.com
  Subject: Re: Inserting Null Value or String Value
  In-Reply-To: <E17wSFC-0003rO-00@hapcibeno.freestart.hu>
  References: <20021001165453.882.qmail@web14410.mail.yahoo.com> <E17wSFC-0003rO-00@hapcibeno.freestart.hu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox