9951 explained code solutions for 126 technologies


php-mysqlHow to store a boolean value in a MySQL database using PHP?


MySQL databases can store boolean values using the TINYINT data type. This data type can store values of 0 (false) or 1 (true). To store a boolean value in a MySQL database using PHP, the following code can be used:

$boolean_value = true;
$sql = "INSERT INTO table_name (boolean_column) VALUES (?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $boolean_value);
$stmt->execute();

The code above:

  1. Declares a variable $boolean_value and assigns it a boolean value of true.
  2. Creates an SQL query to insert a value into a table.
  3. Prepares the SQL query for execution.
  4. Binds the boolean value to the SQL query.
  5. Executes the SQL query.

Helpful links

Edit this code on GitHub