gpt4 book ai didi

php - 如何在准备好的语句中使用文字?

转载 作者:行者123 更新时间:2023-11-30 00:38:51 25 4
gpt4 key购买 nike

在 MySQL 中有 DEFAULT关键字让数据库知道它应该只使用列定义的默认值:

Use the keyword DEFAULT to set a column explicitly to its default value. This makes it easier to write INSERT statements that assign values to all but a few columns, because it enables you to avoid writing an incomplete VALUES list that does not include a value for each column in the table. Otherwise, you would have to write out the list of column names corresponding to each value in the VALUES list.

理论上,这对于准备好的 INSERT 语句来说很好。问题是 PHP doesn't seem to have一种绑定(bind) ? 的方法占位符为 DEFAULT关键字,就像 NULL 一样(PDO::PARAM_NULL)。那么,如何才能实现完全通用且可重用的准备好的语句,其中某些参数有时应该接受默认值(事先不知道)?

我目前正在研究一个薄的数据库模型层,这使我无法充分利用准备好的语句,如果您想将其用于批量插入,这是一个问题。

最佳答案

只需从 INSERT 语句中省略您想要用默认值填充的列,明确指定您确实放置非必要值的列名称 -默认值。

编辑:如果你想要一个“完全通用的、因此可重用的、准备好的声明”,你就不能拥有它。最接近您要求的是以下解决方法:

INSERT INTO TABLE theTable (column1, column2, column3) VALUES
(COALESCE(?,DEFAULT(column1)),
(COALESCE(?,DEFAULT(column2)),
(COALESCE(?,DEFAULT(column3)))

这种方法意味着当您需要 DEFAULT 时您可以传递 NULL,但由于列名是固定的,因此可以部分满足您的要求。

关于php - 如何在准备好的语句中使用文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21955071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com