Re: bool type (between MySQL and PHP)
| From: | Daniel Brown | Date: | Tue, 25 Jan 2011 23:09:56 +0000 |
| Subject: | Re: bool type (between MySQL and PHP) | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-47654@lists.php.net to get a copy of this message | ||
On Tue, Jan 25, 2011 at 17:40, Donovan Brooke <lists@euca.us> wrote:
>
> Thanks Dan,
>
> I think the error would still persist using DEFAULT.. because as mentioned,
> I don't think MySQL likes:
>
> UPDATE tablename SET vbool=
>
> ..which is not great news for me since I'd like to avoid having to redo
> all my bool's in PHP (input checking, updates, add's etc.. ;-) ENUM would
> require that route as well I guess.
Oh, sorry, I must've skimmed that part of the message. If you're
not changing the value from the default, just omit it from the query.
You can't send an empty value. If, for whatever reason, you MUST send
the
vbool column data in your UPDATE query, then define it: 0 =
false, 1 = true (of course). Two easy workarounds here:
<?php
/**
* Method #1 - note the triple-equals. If you set $tvar to anything
* other than boolean true/false (such as 0/1), drop it to
double-equals.
*/
if ($tvar === true) {
mysql_query("UPDATE tablename SET vbool='1'");
}
/**
* Method #2 - do it on the fly with a ternary operator.
*/
$sql = "UPDATE tablename SET vbool='".isset($tvar) && $tvar ?
'1' : '0'."'";
?>
--
</Daniel P. Brown>
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/