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...
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;
How can you do that?
Look this example:
$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;
The bindParam() function binds a parameter to a named or question mark placeholder in an SQL statement. The bindParam () function is used to pass variable not value.
In layman's terms, PDO prepared statements work like this: Prepare an SQL query with empty values as placeholders with either a question mark or a variable name with a colon preceding it for each value. Bind values or variables to the placeholders. Execute query simultaneously.
PDO::PARAM_STR (int) Represents the SQL CHAR, VARCHAR, or other string data type. PDO::PARAM_STR_NATL (int) Flag to denote a string uses the national character set.
If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object.
The keyword DEFAULT
can't be used inside an expression, it can only replace the entire expression.
However, the function DEFAULT()
can be used anywhere.
So replace DEFAULT
in your example with DEFAULT(User_Login__Is_Active)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With