mysqli准备的语句,使用绑定参数插入NULL [英] mysqli prepared statements, insert NULL using bind params
问题描述
有人知道是否可以使用MYSQLI bind_param将NULL插入到列中.
Does anyone know if it is possible to insert NULL into a column with MYSQLI bind_param.
我遇到一种情况,有时我想将bind_param
中的列设置为null.
像这样...
I have a situation where sometimes I want to set a column to null in bind_param
.
Like so...
$column2 = "NULL";
$insert_data->bind_param('ss', $column1,$column2);
很显然,这只是将NULL作为字符串写入到列中.
我已经尝试过null
和\0
,但是它们不起作用.
Obviously, this just writes NULL to the column as a string.
I've tried null
and \0
but they don't work.
我想知道是否可以将$column2
设为相等,这将导致它提交并返回实际的空值.
I would like to know if there is some value I can make $column2
equal that will cause it to submit and actual null value instead.
否则,我将必须动态构建类型和参数列表,并使用call_user_func_array
创建绑定,以使如果列为null则不写入列.我可以这样做,但是会导致很多多余的代码,所以我只是想知道是否可以避免.
Otherwise, I'll have to dynamically build the type and parameter lists and use call_user_func_array
to create my binding such that columns aren't written if they are null. This I can do but it leads to a lot of fiddly extra code so I was just wondering if this can be avoided.
推荐答案
使用:$column2 = null;
不:$column2 = "null";
这篇关于mysqli准备的语句,使用绑定参数插入NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!