NotionCommotion Posted January 21, 2019 Share Posted January 21, 2019 MySQL cannot store Boolean values and thus I use 0/1. Is there much benefit to cast them as Boolean immediately after querying the database, do whatever PHP processing is required, and then convert them back to 0/1 before writing to the database? One benefit is I can type declare my arguments, but I am debating whether it is worth it. Thanks Link to comment Share on other sites More sharing options...
gw1500se Posted January 21, 2019 Share Posted January 21, 2019 My SQL does accept the constants "TRUE" and "FALSE" if that helps. Link to comment Share on other sites More sharing options...
ginerjm Posted January 21, 2019 Share Posted January 21, 2019 PHP does inherently treat the MySQL values of 0 and 1 as true and false. So what you are asking has already been answered. Link to comment Share on other sites More sharing options...
gw1500se Posted January 21, 2019 Share Posted January 21, 2019 It really doesn't change what or how they are actually stored but it would may your code more readable. Exactly what are you really asking? Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 21, 2019 Author Share Posted January 21, 2019 23 minutes ago, gw1500se said: My SQL does accept the constants "TRUE" and "FALSE" if that helps. Thank you. I was unaware. Are you saying one can do $pdo->prepare($sql)->execute([123, true, false]); ? 6 minutes ago, ginerjm said: PHP does inherently treat the MySQL values of 0 and 1 as true and false. So what you are asking has already been answered. I am not asking from a MySQL prospective but a PHP prospective. In the past, I never explicitly declared variable argument and returned types, and found that it got me in a lot of trouble. I am asking whether it is good practice to perform boolval($mysqlOutput) or (bool) $mysqlOutput on data received from MySQL before processing the data with PHP. Link to comment Share on other sites More sharing options...
gw1500se Posted January 21, 2019 Share Posted January 21, 2019 5 minutes ago, NotionCommotion said: Thank you. I was unaware. Are you saying one can do $pdo->prepare($sql)->execute([123, true, false]); ? Close. I think using execute will throw an error. I believe you have to use "bindValue". Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 21, 2019 Author Share Posted January 21, 2019 Ah yes, I do recall that now. I've never really used bindValue, so have been kind of stuck. What about my original question? Is what I am asking clear? On a related note, do you know if it is possible to make PDO output data as boolean? I searched and it appears not to be possible. Link to comment Share on other sites More sharing options...
gw1500se Posted January 21, 2019 Share Posted January 21, 2019 I guess it is not clear. In both MySQL and PHP true/false are still represented as 1/0 integers internally. Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 21, 2019 Author Share Posted January 21, 2019 Yes they are but 1/0 will not work with the following: public function foo(bool $status):bool; Link to comment Share on other sites More sharing options...
gw1500se Posted January 21, 2019 Share Posted January 21, 2019 Just cast to 'bool' in your call. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.