PHP PDO:为什么插入NULL会产生0 [英] Php PDO: Why does Inserting NULL yields to 0

查看:85
本文介绍了PHP PDO:为什么插入NULL会产生0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了可以在网上找到的所有可能的帮助,但是仍然存在使用PHP PDO的INSERT NULL问题.

I searched any possible help that can be found online but still the problem with INSERT NULL using PHP PDO persists.

该脚本是一个csvupload脚本,最初来自此处导入CSV到MySQL

The script is a csvupload script originally came from here Import CSV into MySQL

为了使故事简短,让我介绍可能的原因.

To make the story short, Let me present the possible cause..

if($linearray[4]=='Unknown')
    $linearray[4]=null;
$linemysql = implode("','",$linearray);
$linemysql = "'".$linemysql."'";
$setsu->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$tsuika = $setsu->prepare("INSERT INTO tablename (SubAgentID, BookID, AgentID, SubAgentName, Risk, Area, CurrentBalance) VALUES ($linemysql)");
$tsuika -> bindValue(':Risk', null, PDO::PARAM_INT);
$tsuika ->execute();

通过查看上面的代码,我在prepare语句上显式设置了字段值. 在phpmyadmin上,Risk字段接受NULL,将默认值设置为NULL,并且没有问题.但是当用PHP进行INSERT时,它得到的值为0.为什么?

Looking the code above, I explicitly set the field values on the prepare statment. On phpmyadmin the Risk field accepts NULL, set the default value to NULL, and has no problems. But when doing INSERT with PHP the value it gets is 0. Why?

在插入之前,我已经回显了它,并且如果$ linearray [4]字段包含Unknown,它会将其转换为NULL,产生该部分的''.

Before Inserting, I echoed it and if the field $linearray[4] contains Unknown, it converts it to NULL yielding, '' for that part.

表结构

CREATE TABLE IF NOT EXISTS `subagentdb` (
`SubAgentID` int(10) NOT NULL AUTO_INCREMENT,
  `BookID` int(10) NOT NULL,
  `AgentID` int(10) NOT NULL,
  `SubAgentName` varchar(30) NOT NULL,
  `Risk` float DEFAULT NULL,
  `Area` varchar(20) NOT NULL,
  `CurrentBalance` float NOT NULL,
  PRIMARY KEY (`SubAgentID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;

推荐答案

仅使用PDO :: PARAM_NULL而不是PDO :: PARAM_INT?我认为NULL会转换为0(INT)而不是null值

just use PDO::PARAM_NULL instead of PDO::PARAM_INT ? I think the NULL is converted to 0 (INT) instead of null value

这篇关于PHP PDO:为什么插入NULL会产生0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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