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.)