php mysql准备语句将0传递到十进制列,其中允许NULL

php mysql准备语句将0传递到十进制列,其中允许NULL

问题描述:

I have a Mysql table with a decimal column that allows NULLs. If I pass 0 as double in a PHP's prepared statement, my 0 turns NULL in the Mysql column.

How can I get the value 0 instead of NULL?

PS: I use the following code to insert the value

$query2 = "insert into TableName
    (Id, anotherId, date,
    `MinValue`, `MaxValue`,
    `oneMoreId`, `Comment`)
  values
    (?, ?, '2016-06-23',
    ?, ?,
    ?, ?)";

$stmt = $connection->prepare($query2);
$stmt->bind_param('iiddis', $Id, $anotherId,
        $minValue, $maxValue, $oneMoreId,
        $comment);
$stmt->execute();

EDIT: after answer by @YourCommonSense, I figured there was an error in my code. Above the lines that I posted, I had a change of value from 0 to NULL. My fault, sorry, guys!

我有一个带有十进制列的Mysql表,允许 NULL code>。 如果我在PHP的预处理语句中将0传递为double,则我的0在Mysql列中变为NULL。 p>

如何获取值0而不是 NULL code>? p>

PS:我使用以下代码插入 value p>

  $ query2 =“insert into TableName 
(Id,anotherId,date,
`MinValue`,`MaxValue`,
`oneMoreId`,`Comment`  )
值
(?,?,'2016-06-23',
?,?,
?,?)“; 
 
 $ stmt = $ connection-> prepare($ query2)  ; 
 $ stmt-> bind_param('iiddis',$ Id,$ anotherId,
 $ minValue,$ maxValue,$ oneMoreId,
 $ comment); 
 $ stmt-> execute(); 
   code>  pre> 
 
 

编辑:在@YourCommonSense回答后,我发现我的代码中有错误。 在我发布的行之上,我将值从0更改为 NULL code>。 我的错,抱歉,伙计们! p> div>

If I pass 0 as double in a PHP's prepared statement, my 0 turns NULL in the Mysql column.

This is not true.

If you pass 0, then it gets stored as 0.
But if you pass NULL, then it gets stored as NULL.

Therefore, you are passing NULL value, not 0.
Check your values and cast them if necessary.