Home > Database > Mysql Tutorial > How to Preserve NULL Values in MySQLi Prepared Statements?

How to Preserve NULL Values in MySQLi Prepared Statements?

Susan Sarandon
Release: 2024-11-03 01:40:29
Original
891 people have browsed it

How to Preserve NULL Values in MySQLi Prepared Statements?

Passing NULL in MySQLi Prepared Statements

In a MySQLi prepared statement, NULL values are automatically converted to '' (strings) or 0 (integers). However, it may be desired to store NULL values without any conversion.

Solution:

To preserve NULL values, the mysql NULL safe operator must be used. This operator is written as '<=>' and checks if the value is equal to NULL.

Example:

The following code snippet demonstrates the use of the NULL safe operator to select products with NULL prices:

<code class="php">$price = NULL; // NOTE: no quotes - using php NULL
$stmt = $mysqli->prepare("SELECT id FROM product WHERE price <=> ?"); // Will select products where the price is null
$stmt->bind_param('i', $price);</code>
Copy after login

The above is the detailed content of How to Preserve NULL Values in MySQLi Prepared Statements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template