PHP/MySQL 插入空值

发布于 2024-10-24 04:08:34 字数 385 浏览 1 评论 0原文

我正在从 1 个表中读取数据,更改一些字段,然后写入另一个表,当我希望在数据库中插入 null 时(该字段允许使用 null 值),如果插入且其中一个数组值为 null,则不会发生任何情况。它看起来有点像这样:

$results = mysql_query("select * from mytable");
while ($row = mysql_fetch_assoc($results) {
    mysql_query("insert into table2 (f1, f2) values ('{$row['string_field']}', {$row['null_field']});
}

并非每一行都有空值,并且在我的查询中还有更多字段和 2 列,它们可能为空,也可能不为空

I am reading from 1 table, changing some fields then writing to another table, nothing happens if inserting and one of the array values is null when I would like it to insert null in the database (null values are allowed for the field). It looks a bit like this:

$results = mysql_query("select * from mytable");
while ($row = mysql_fetch_assoc($results) {
    mysql_query("insert into table2 (f1, f2) values ('{$row['string_field']}', {$row['null_field']});
}

Not every row has a null value and in my query there are more fields and 2 columns which may or may not be null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合久必婚 2024-10-31 04:08:34

这是一个使用准备好的语句确实可以帮您省去一些麻烦的例子。

在 MySQL 中,为了插入空值,您必须在 INSERT 时指定它,或者将该字段保留在外面,这需要额外的分支:

INSERT INTO table2 (f1, f2)
  VALUES ('String Value', NULL);

但是,如果您想在该字段中插入值,则必须现在分支您的代码以添加单引号:

INSERT INTO table2 (f1, f2)
  VALUES ('String Value', 'String Value');

准备好的语句会自动为您执行此操作。他们知道 string(0) ""null 之间的区别,并正确编写您的查询:

$stmt = $mysqli->prepare("INSERT INTO table2 (f1, f2) VALUES (?, ?)");
$stmt->bind_param('ss', $field1, $field2);

$field1 = "String Value";
$field2 = null;

$stmt->execute();

它会为您转义您的字段,确保您不会忘记绑定一个参数。没有理由继续使用 mysql 扩展。使用mysqli,它是准备好的语句。你将为自己节省一个痛苦的世界。

This is one example where using prepared statements really saves you some trouble.

In MySQL, in order to insert a null value, you must specify it at INSERT time or leave the field out which requires additional branching:

INSERT INTO table2 (f1, f2)
  VALUES ('String Value', NULL);

However, if you want to insert a value in that field, you must now branch your code to add the single quotes:

INSERT INTO table2 (f1, f2)
  VALUES ('String Value', 'String Value');

Prepared statements automatically do that for you. They know the difference between string(0) "" and null and write your query appropriately:

$stmt = $mysqli->prepare("INSERT INTO table2 (f1, f2) VALUES (?, ?)");
$stmt->bind_param('ss', $field1, $field2);

$field1 = "String Value";
$field2 = null;

$stmt->execute();

It escapes your fields for you, makes sure that you don't forget to bind a parameter. There is no reason to stay with the mysql extension. Use mysqli and it's prepared statements instead. You'll save yourself a world of pain.

哑剧 2024-10-31 04:08:34

对于可接受 NULL 的字段,您可以使用 var_export($var, true) 输出 stringintegerNULL 文字。请注意,您不会用引号将输出括起来,因为它们会自动添加或省略。

例如:

mysql_query("insert into table2 (f1, f2) values ('{$row['string_field']}', ".var_export($row['null_field'], true).")");

For fields where NULL is acceptable, you could use var_export($var, true) to output the string, integer, or NULL literal. Note that you would not surround the output with quotes because they will be automatically added or omitted.

For example:

mysql_query("insert into table2 (f1, f2) values ('{$row['string_field']}', ".var_export($row['null_field'], true).")");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文