How to get the last inserted record on a table with pdo_mysql?

I tried an experiment with this, and it is working:

$val = “testing”;
$sql = “INSERT INTO tableSets (sets_nameunique) VALUES (:val)”;
$stmt = $pdo->prepare($sql);
$stmt->bindParam(‘:val’, $val, PDO::PARAM_INT);
$stmt->execute();
$lastId = $pdo->lastInsertId();

echo $lastId;

I’m not sure why the other methods are not working, but I’ll run with this one that is.

That’s strange, both in that just using bindParam() instead of passing an array seems to make it work, and in that casting your string for insertion into a varchar(150) column to an integer lets it work.

I should add - I use individual bindParam statements to pass parameters into queries, but talked about the array instead because you mentioned you have a lot of parameters to pass in.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.