0

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

5
  • What's in between the PHP code and the database? i.e. how are these values interpolated? Commented Jun 21, 2012 at 9:13
  • what wrong with your query? parent_id = NULL will update the field into null. Commented Jun 21, 2012 at 9:14
  • the query is correct, but I want to set the value with a variable, the code between is not that simple, it's build with classes Commented Jun 21, 2012 at 9:14
  • @Ruben: Raw PHP won't give you that level of abstraction you seem to be using. We need more details on what you actually use to actually help you. Commented Jun 21, 2012 at 9:18
  • ok, I think something goes wrong in my database class, I'll check that out first Commented Jun 21, 2012 at 9:25

2 Answers 2

2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

what's the difference between NULL and null?
@johntotetwoo it depends on his query editor MySQL Query Browser takes as simple null
@Sudantha sql is case insensitive language. How do u say that query editors requires NULL as null?
@Prathap yes you are correct its not about SQL im talking about how query editor interprets that
@Sudantha may I know some query editor that looks for case sensitive like null as NULL. It will be gud to know...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.