Re: bool type (between MySQL and PHP)
| From: | Donovan Brooke | Date: | Tue, 25 Jan 2011 23:38:19 +0000 |
| Subject: | Re: bool type (between MySQL and PHP) | ||
| References: | 1 2 3 4 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-47655@lists.php.net to get a copy of this message | ||
Daniel Brown wrote:
On Tue, Jan 25, 2011 at 17:40, Donovan Brooke<lists@euca.us> wrote:Thanks Dan, I'm sure that would work, but it appears I figured out where I went wrong... Read below at your own risk of becoming confused. ;-) As of now, "false" causes MySQL to input the correct value, which is "0". I was originally trying to initialize/set the var inside the checker: if (filter_input(INPUT_POST,'f_bool',FILTER_VALIDATE_BOOLEAN)) { $vbool = $_POST['f_bool']; } else { <some error code> } The very strange thing is that, though the "else" statement was never thrown, anything inside the if (true) statement was not enacting... thus MySQL was getting an empty value, because the variable was never getting set. Strange, anyway, I bypassed the filter and now MySQL accepts false boolean's. I'll now have to figure out how to correctly validate a boolean!... (tomorrow) ;-) Thanks for the assist. Donovan -- D BrookeThanks 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'renot 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 thevboolcolumn 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 todouble-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'."'";?>