Re: PDO Parameter types // PDO::PARAM_AUTO

From: Date: Thu, 18 May 2017 05:12:58 +0000
Subject: Re: PDO Parameter types // PDO::PARAM_AUTO
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-99090@lists.php.net to get a copy of this message
Hi Dorin, On 15/05/2017 18:22, Dorin Marcoci wrote: > Currently PDOStatement::bindValue is declared as > > public bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int > $data_type = PDO::PARAM_STR ] ) > > where third parameter is data type, string type as default. > > It may be useful in terms of providing meta info and handle it accordingly, > like quote, cast or do some additional stuff. > > But there are DBMSes where this "feature" is more a burden than a helper. Please elaborate. > It's because after PDO::prepare, driver and server already knows all > statement parameters and expected types. > > Yes, it can be "forced", saying to pass for a integer parameter a string > value, server will convert it to required type. > > It is convenient in most cases to pass them as strings, for example by > simply assign all params in a cycle before execute. The thing is: the server (with non-emulated prepares) knows the data type it's expecting. PHP has no knowledge of it, so you are expected to do so if the default (string) is not ok. > But when implementing FB Boolean data type parameters and doing ->bindValue > ('bool_param', false) I get an empty string internally. > > Yes, true ZVAL is converted as string as '1', but false as an empty string, > not so consistent here ;) That's not true. $p = new PDO("pgsql:dbname=postgres"); $p->prepare("SELECT ?::bool"); $s->bindValue(1, false); $s->execute(); var_dump($s->fetchColumn());' outputs: bool(false) What are you referring to? > That's why I think, it will be great to have a special type like > PDO::PARAM_AUTO and a config flag to set it as default instead of PARAM_STR. Config flags are evil as it's one more thing you'd need to be aware of and could change depending on the environment you run your script or library on. Cheers -- Matteo Beccati Development & Consulting - http://www.beccati.com/

« previous php.internals (#99090) next »