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:
- Declares a variable
$boolean_value
and assigns it a boolean value oftrue
. - Creates an SQL query to insert a value into a table.
- Prepares the SQL query for execution.
- Binds the boolean value to the SQL query.
- Executes the SQL query.
Helpful links
More of Php Mysql
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to join tables with PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to fetch data from MySQL in PHP?
See more codes...