|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-11-26 14:42 UTC] phoeniks2k at mail dot ru
Description: ------------ I can't find how to bind NULL with mysqli_stmt_bind_param() Even when i use string and pass NULL it's converted to '' (empty string), int is converted to 0. But i need to insert NULLs. If it's a doc miss, please describe how to use NULLs, othewise fix mysqli extension PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 16:00:01 2025 UTC |
Sorry I misused a little. Troubles are not in INSERTs, but in SELECTS <?php $mysqli = new mysqli('localhost', 'p', 'p', 'p'); $code = 1; $language = NULL; $stmt = $mysqli->prepare("SELECT id, val FROM table_name WHERE id = ? AND (val = ? OR ? IS NULL)"); $stmt->bind_param('iss', $code, $language, $language); //$stmt->bind_param('i', $code); $stmt->execute(); $stmt->bind_result($col1, $col2); while ($stmt->fetch()) { printf("%s %s\n", $col1, $col2); } $stmt->close(); $mysqli->close(); ?> Table contains data +----+--------+ | id | val | +----+--------+ | 1 | 2 | | 1 | [NULL] | +----+--------+ The statement above returns 0 rows. But query SELECT id, val FROM table_name WHERE id = 1 AND (val = NULL OR NULL IS NULL) returns 1 row with NULL as val (as it should realy do) AND SELECT id, val FROM table_name WHERE id = 1 AND (val = 2 OR 2 IS NULL) returns 1 row with 2 as val (as it should realy do)Here placeholder is *NOT* an indentifier. Not a column name, but a variable/ Here is a rewritten example prepare is smth like SET @BOUND_VAR = ?; bind sets variable to NULL $code = NULL; $stmt->bind_param('i', $code); SELECT id, val FROM table_name WHERE id = 1 AND (val = @BOUND_VAR OR @BOUND_VAR IS NULL) Now @BOUND_VAR is threated as 0 instead of NULL. that's incorrect