问题描述:

I have this query:

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

And I want to set the parent_id to NULL with php, but how do I do this?

These are wrong:

$term->parent_id = 'NULL'; -> this is a string

$term->parent_id = NULL; -> this is empty

$term->parent_id = 0; -> this is an integer

网友答案:

there was a problem in my database class, that's why my parent_id value was always empty
this is how I fixed it:

old code:

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;
    }

new code:

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;
    }
网友答案:

Query should be as :

some query editors requires NULL as 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
相关阅读:
Top