From the PHP manual, it states that:
PDO::ATTR_EMULATE_PREPARES Enables or disables emulation of prepared statements. Some drivers do not support native prepared statements or have limited support for them. Use this setting to force PDO to either always emulate prepared statements (if TRUE), or to try to use native prepared statements (if FALSE). It will always fall back to emulating the prepared statement if the driver cannot successfully prepare the current query. Requires bool.
I have a query that goes like this:
$type = PDO::PARAM_INT;
if($_POST['code'] == "") $type = PDO::PARAM_NULL;
$stmt = $dbh->prepare("UPDATE Product SET code=? WHERE id=?");
$stmt->bindValue(1, $_POST['code'], $type);
$stmt->bindValue(2, $_SESSION['id'], PDO::PARAM_INT);
$stmt->execute();
I realise that without setting the following statement, I would get a '0'
instead of a NULL
value (when $_POST['code'] == ""
) into my database using the code above. Why is that so?
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
UPDATE:
Although disabling of the emulation works for me, I would prefer to use it because like the OP in this question, basically all my queries are only executed once per script execution. Therefore, having the statements prepared does not gain me any advantage and creates unnecessary calls to the database as shown in the general query log below:
Emulation Disabled
22 Connect user@localhost on Database
22 Prepare UPDATE Product SET code=? WHERE id=?
22 Execute UPDATE Product SET code='abc' WHERE id='123'
22 Close stmt
22 Quit
Emulation Enabled
22 Connect user@localhost on Database
22 Query UPDATE Product SET code='abc' WHERE id='123'
22 Quit
Any help to solve the NULL
issue is greatly appreciated.