mysqli准备的语句,使用绑定参数插入NULL [英] mysqli prepared statements, insert NULL using bind params

查看:63
本文介绍了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\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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆