MySQL 将 NULL 值插入到 INT 列中 [英] MySQL Inserting NULL Value into INT Column

查看:36
本文介绍了MySQL 将 NULL 值插入到 INT 列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过这个问题.我找到了这个帖子,但是它并没有解决我的问题,因为我的列确实允许为空.

I know that this question has been asked before. I found the post, however, it did not solve my problem as my column does allow null.

我想在我的整数列中插入一个 NULL 值.

I want to insert a NULL value into my integer column.

if ($startYear == "") {
    $startYear = NULL;
}

mysqli_query($con, "INSERT INTO Application (startYear) VALUES ('".$startYear."')");

在你问之前,我已经检查过 $startYear 是否曾经为空,而且确实如此,所以这应该可行.

Before you ask, I have checked if $startYear is ever empty, and it is, so this should work.

当我运行此查询时,它会在数据库行中输入一个 0.它不会像我想要的那样插入 NULL.

When I run this query it inputs a 0 into the database row. It does not insert NULL like I want.

这是我的专栏结构:

http://i.imgur.com/XuWq9Km.png

一件重要的事情是,我不能修改列的结构.这是禁区.

提前致谢.

推荐答案

您要插入一个 PHP 空值,该值在字符串上下文中使用时会变成空字符串 - 这就是最初的内容.您必须插入一个带有字母 n, u,l,l` 的文字字符串才能工作:

you're inserting a PHP null value, which when used in a string context becomes an empty string - which is what was originally. You have to insert a literal string with the letters n, u,l,l` for this to work:

if ($startYear == '') {
   $startYear = 'NULL'; // string null
}
$sql = "INSERT ..... VALUES ($startYear)";

哪个会产生

INSERT ... VALUES (NULL)

展开:

   $foo = null; // actual PHP null value
   $bar = "$foo"; // insert this php null into a string
   echo strlen($bar); // returns 0

   $foo = 'null' // string with 4 letters: n,u,l,l
   $bar = "$foo";
   echo strlen($bar); // returns 4

这篇关于MySQL 将 NULL 值插入到 INT 列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆