Re: bool type (between MySQL and PHP)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 25, 2011 at 17:40, Donovan Brooke <lists@xxxxxxx> 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/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux