How do I reference this in a php code query? Would this code work without the above being added to options?
$id = $pdo->lastInsertId();
Note: Since I’m using PHP to run the query, I thought this should belong in the PHP forum. If I am wrong, please let me know and I will move it to the Database forum.
<?php
declare(strict_types=1);
// in case anyone was wondering something like
$val = 5;
$sql = "REPLACE table (column) VALUES (:val)";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':val', $val, PDO::PARAM_INT);
$stmt->execute();
$lastId = $dbh->lastInsertId();
// will return the last inserted id, whether the record was replaced or simply inserted
// the REPLACE syntax, simply inserts, or deletes > inserts
// so lastInsertId() still works
// refer to // http://mysql.com/doc/refman/5.0/en/replace.html for REPLACE usage
I don’t know, I tend to not lump all the statements in together. Does your query actually execute?
In the first code, you’re not including the : at the start of each token name.
Not if you build an indexed array containing the parameter name and value, and then pass that array into the execute - as you do above, but with the full parameter including the opening colon.
Your query only inserts a new row, it doesn’t do any replacing. I can’t say I’ve ever tried looking at the insert-id after a REPLACE query, I’ve probably just presumed it won’t return anything because nothing is being inserted. At that point the “rows affected” count might be more appropriate, as that reflects any rows that changed as a result of the query.
Bind-Parameters would work with any type of query that has replaceable parameters, whether its inserting, replacing or just retrieving data.
I’m pretty sure that comment is directed at the use of lastInsertId() rather than bind-params.
I guess the next question must be what PDO driver are you using? There are notes on the doc page that not all PDO drivers support lastInsertId(), so maybe you’re using one that doesn’t.