mysqli 准备语句,使用绑定参数插入 NULL

有谁知道是否可以使用 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 但它们不起作用.

Obviously, this just writes NULL to the column as a string. I've tried null and 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 来创建我的绑定,这样如果列为空,则不会写入它们.我可以这样做,但它会导致大量繁琐的额外代码,所以我只是想知道是否可以避免这种情况.

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.

推荐答案

use: $column2 = null; not: $column2 = "null";

use: $column2 = null; not: $column2 = "null";

相关文章