Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo boolean field as yes/no or other values

Tags:

php

mysql

I have a field on a read only SQL table called attended can only hold two values either 0 or 1.

This is how I am printing the field at the moment:

  echo "<td>" . $row['attended'] . "</td>";

It only returns 0 or 1 - the value in the attended field. How can I have it return no for 0 (i.e not attended) or yes for 1 (i.e. attended).

Many thanks in advance!

like image 766
methuselah Avatar asked Mar 03 '12 15:03

methuselah


People also ask

How do you create a Yes No boolean field in SQL Server?

In SQL you use 0 and 1 to set a bit field (just as a yes/no field in Access). In Management Studio it displays as a false/true value (at least in recent versions). When accessing the database through ASP.NET it will expose the field as a boolean value.

How do you give a boolean value in SQL?

You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.

How do you create a boolean column in SQL Server?

In SQL Server, a Boolean Datatype can be created by means of keeping BIT datatype. Though it is a numeric datatype, it can accept either 0 or 1 or NULL values only. Hence easily we can assign FALSE values to 0 and TRUE values to 1. This will provide the boolean nature for a data type.


1 Answers

You can use the ternary operator (also known as the conditional operator in some languages) ?::

echo '<td>' . ($row['attended'] ? 'yes' : 'no') . '</td>';

This operator is mentioned in the manual page Comparison Operators under the heading "Ternary Operator".

like image 190
Mark Byers Avatar answered Sep 30 '22 13:09

Mark Byers