当BindParam中的变量为null时,pdo准备好的语句插入DEFAULT [英] pdo prepared statement insert DEFAULT when the variable in BindParam is null
问题描述
我有这个问题: 我正在使用PDO准备的语句. 我想绑定一个变量,但如果该变量为NULL,则必须在MYSQL中插入该字段的默认值...
I have this problem: I'm using PDO prepared statement.... I want to BIND a variable BUT if the variable is NULL it have to INSERT in MYSQL the DEFAULT VALUE of the field...
我正在尝试 IFNULL(:User_Login__Is_Active,DEFAULT), 我也尝试过: COALESCE(:User_Login__Is_Active,DEFAULT), 同样的错误: PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;
I'm trying with IFNULL(:User_Login__Is_Active, DEFAULT), And I tried also: COALESCE(:User_Login__Is_Active, DEFAULT), Same error: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
你怎么做到的?
看这个例子:
$stmt = $this->pdo->prepare('INSERT INTO user_login
( User_Login__ID,
User_Login__Is_Active,
User_Login__Created_Date )
VALUES (
:User_Login__ID,
IFNULL(:User_Login__Is_Active, DEFAULT),
:User_Login__Created_Date )');
$stmt->bindParam(':User_Login__ID', $this->User_Login__ID, PDO::PARAM_INT);
$stmt->bindParam(':User_Login__Is_Active', $this->User_Login__Is_Active, PDO::PARAM_STR, 100);
$stmt->bindParam(':User_Login__Created_Date', $this->User_Login__Created_Date, PDO::PARAM_STR, 100);
$this->User_Login__Is_Active = null;
推荐答案
关键字DEFAULT
不能在表达式内部使用,只能替换整个表达式.
The keyword DEFAULT
can't be used inside an expression, it can only replace the entire expression.
但是,函数 DEFAULT()
可以在任何地方使用
However, the function DEFAULT()
can be used anywhere.
因此将示例中的DEFAULT
替换为DEFAULT(User_Login__Is_Active)
.
So replace DEFAULT
in your example with DEFAULT(User_Login__Is_Active)
.
这篇关于当BindParam中的变量为null时,pdo准备好的语句插入DEFAULT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!