问题描述
我正在通过URL将值传递到PHP页面中,并在MySQLi查询中使用它们.问题在于,对于一个整数列,空字符串实际上会转换为零,而实际上它必须为NULL.
I'm passing values into a PHP page through the URL and using them in queries with MySQLi. The problem is that an empty string gets converted to zero for an integer column, when in fact it needs to be NULL.
如何通过URL中传递的参数使用PHP/MySQLi将NULL插入整数列?
How do I get NULL into an integer column with PHP/MySQLi, from a param passed in the URL?
更新:这是我如何使用传入参数和准备好的语句进行插入的示例.在数据库上我需要为NULL的是$ specialtyType.
Update:Here's an example of how I'd use the incoming parameter and a prepared statement to do the insert. The one I need to be NULL on the DB is $specialtyType.
function InsertItem($itemID, $ownerID, $specialtyType)
{
$this->insertItemStmt->bind_param("ssi", $itemID, $ownerID, $specialtyType);
$this->insertItemStmt->execute();
如果我将Jason的建议行添加到bind_param之前(设置一个中间变量,并在bind_param中使用它而不是$ specialty_type),则它的结尾将在execute()之后包含0.
If I add Jason's suggested line before the bind_param (setting up an intermediate variable and using it in the bind_param instead of $specialty_type), it winds up containing 0 after the execute().
推荐答案
$int_value = empty($_GET['int_value']) ? NULL : (int)$_GET['int_value'];
在INSERT
中的列中使用$int_value
.
这篇关于如何使用PHP/MySQLi将NULL插入MySQL整数列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!