php - 使用 mysql 中的 php 更新值 NULL

标签 php mysql 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

我想用 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/

相关文章:

php - 访问来自 PHP/JQuery.ajax 的传入 PUT 数据以及如何处理它?

php - 解码 JSON + PHP 时遇到问题

PHP session 注销错误

php - 输入表单不保存在 phpmyadmin 中

java - 数据截断 : Incorrect datetime value: 'null' in Java

kotlin - 如果构造函数不能返回 null,为什么我需要在初始化后进行类型检查?

php - fopen+fwrite 比 shell_exec 快吗?

mysql - MAMP Pro 2.0.2 - 在 dropbox 上同步的 Mysql 数据库不知何故变得不同步?

mysql - 硬 SQL 查询 - 在另一个表中没有匹配的行或具有未在另一个表中发布的项目

Java 空检查