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>
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!