2012-06-21 91 views
0

PHP我有这个疑问:更新NULL值与MySQL的

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 

我想设置的PARENT_ID与PHP的NULL,但我要如何做到这一点?
这些都是错误的:

$term->parent_id = 'NULL'; - >这是一个字符串
$term->parent_id = NULL; - >这是空
$term->parent_id = 0; - >这是一个整数

+0

PHP代码和数据库之间有什么?即这些值是如何内插的? – symcbean

+0

查询有什么问题? 'parent_id = NULL'会将该字段更新为null。 –

+0

该查询是正确的,但我想设置一个变量的值,其间的代码并不是那么简单,它是用类构建的 – Ruben

回答

1

有我的数据库类的问题,这就是为什么我PARENT_ID pH值总是空
这是我的固定它:

旧代码:

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

查询应为:

一些查询编辑器要求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 
+1

'NULL'和'null'有什么区别? –

+0

@johntotetwoo它取决于他的查询编辑器'MySQL查询浏览器'尽可能简单null – Sudantha

+0

@Sudantha sql是不区分大小写的语言。你怎么说查询编辑器要求NULL为空? – Prathap