Php 通过mysqli::query中的变量传递null

Php 通过mysqli::query中的变量传递null,php,mysqli,Php,Mysqli,我有一个特殊情况,需要使用mysqli::query而不是mysqli::prepare。我试图通过变量将null传递给存储过程。以下内容适用于准备好的报表: $val = null; $stmt = $this->db->prepare('CALL my_stored_procedure(?, ?)'); $stmt->bind_param('ii', $this->id, $val); etc... 不适用于查询: $val = null; $qry = $this

我有一个特殊情况,需要使用mysqli::query而不是mysqli::prepare。我试图通过变量将null传递给存储过程。以下内容适用于准备好的报表:

$val = null;
$stmt = $this->db->prepare('CALL my_stored_procedure(?, ?)');
$stmt->bind_param('ii', $this->id, $val);
etc...
不适用于查询:

$val = null;
$qry = $this->db->query("CALL my_stored_procedure($this->id, $val)");
etc...
结果:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, )'
我可以显式地传递null ok,但这对我没有好处


同样,在这种情况下,我不能使用事先准备好的陈述。有没有办法将null传递给mysqli查询

您不能使用以下语句:

$procedureArguments = '$this->id, ' . ( is_null($val) ? 'NULL' : $val );
$qry = $this->db->query('CALL my_stored_procedure(' . $procedureArguments . ')');

谢谢你的回答。这也是可行的,因为它传递的是MySQL关键字“NULL”?$val变量在封装在用双引号定义的字符串中时插入到空字符串
'
。传递字符串NULL时,实际上没有插值。使用单引号定义查询也不起作用,因为字符串“$val”将作为过程的参数而不是实际变量值传递。