PHP bind_params为空 [英] PHP bind_params with null

查看:54
本文介绍了PHP bind_params为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果该变量存在,我试图将参数绑定到INSERT INTO MySQLi准备好的语句中,否则插入null.

I am trying to bind params to a INSERT INTO MySQLi prepared statement if that variable exists, otherwise insert null.

这是我所拥有的,但是不起作用:

This is what I have, but it is not working:

if (!empty($name)) {
    $result->bind_param('ss', $name, $url_friendly_name);
} else {
    $result->bind_param('ss', null, null);
}

if (!empty($description)) {
    $result->bind_param('s', $description);
} else {
    $result->bind_param('s', null);
}

有人知道这样做的更好方法吗,或者我的代码只是一个小问题.我正在对准备好的语句中的每个变量执行以上操作.

Does anyone know of a better way of doing this or is there just a minor issue with my code. I am doing the above for each variable in the prepared statement.

推荐答案

bind_param通过引用起作用.这意味着它需要一个变量,然后使用execute中该变量的值.

bind_param works by reference. That means that it takes a variable, then uses the value of that variable in execute.

null不是变量.

尝试将变量设置为null,然后绑定该变量.

Try setting a variable to null and then binding that variable instead.

(也可以考虑使用PDO代替mysqli,其中execute方法可以采用简单的数组,并且可以绕过mysqli的古怪绑定方法.)

(Also consider using PDO instead of mysqli, where the execute method can take a simple array and you can bypass wacky binding methodology of mysqli.)

这篇关于PHP bind_params为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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