sinesine sinesine - 29 days ago 7
MySQL Question

Returning non-integer values in table for tinyint(1) Boolean data type

I'm going to be using the tinyint(1) / Boolean datatype for a field in my database (MySQL) to represent two values 'solo' and 'dual', where 1 will equal dual and 0 will equal solo.

However, I'm wondering how I will eventually display this in a table. Since the data will be stored as 1 and 0 in the database, how could I change this to the corresponding 'dual' or 'solo' when creating tables using HTML, MySQL, and PHP?

Not asking for code but just wondering if this is possible before using the Boolean datatype. Thanks

Answer

With a very simple function:

function from_db_bool($bool){
  return $bool ? 'dual' : 'solo';
}

function to_db_bool($string){
  return strtolower($string) == 'dual' ? 1 : 0;
}

When inserting, use to_db_bool() when printing out from db use from_db_bool()

Comments