我有这个查询:
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;
}
关于php - 使用 mysql 中的 php 更新值 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134984/