我有这个查询:
UPDATE `terms`
SET id = '15',
taxonomy_id = '1',
parent_id = NULL,
level = 0,
position = 1,
active = '1',
time_created = '2012-05-24 09:31:12',
time_updated = '0000-00-00 00:00:00'
WHERE id = 15
我想用 php 将parent_id设置为 NULL,但是我该怎么做呢?
这些是错误的:
$term->parent_id = 'NULL';
->这是一个字符串
$term->parent_id = NULL;
->这是空的
$term->parent_id = 0;
-> 这是一个整数
我的数据库类有问题,这就是为什么我的parent_id值总是空
这就是我修复它的方式:
旧代码:
public function set($data)
{
$this->query .= ' SET ';
if(is_object($data))
$data = get_object_vars($data);
if(is_array($data))
{
$l = count($data);
$t = 1;
foreach($data as $k => $v)
{
$this->query .= $k . ' = ' . ((is_string($v)) ? '''' . $this->_escape($v) . '''' : $this->_escape($v));
if($t < $l)
$this->query .= ',';
$t++;
}
}
else
{
$this->query .= $data;
}
return $this;
}
新代码:
public function set($data)
{
$this->query .= ' SET ';
if(is_object($data))
$data = get_object_vars($data);
if(is_array($data))
{
$l = count($data);
$t = 1;
foreach($data as $k => $v)
{
$this->query .= $k . ' = ';
if(is_string($v))
{
$this->query .= '''' . $this->_escape($v) . '''';
} elseif (is_null($v)) {
$this->query .= 'NULL';
} else {
$this->query .= $this->_escape($v);
}
if($t < $l)
$this->query .= ',';
$t++;
}
}
else
{
$this->query .= $data;
}
return $this;
}
查询应为:
某些查询编辑器要求 NULL 为 null
UPDATE `terms` SET
id = '15',
taxonomy_id = '1',
parent_id = null,
level = 0,
position = 1,
active = '1',
time_created = '2012-05-24 09:31:12',
time_updated = '0000-00-00 00:00:00'
WHERE
id = 15