Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch number of affected rows and last inserted ID using PDO statement

Tags:

php

pdo

How can I display the numbers of affected rows in this:

$sql = $conn->prepare ("UPDATE countries SET country=:country");
$sql->bindValue(":country", "blablaa");
$sql->execute();

And how can I show the last inserted ID with this:

$sql = $conn->prepare ("INSERT INTO countries (country) VALUES (:country)");
$sql->bindValue(":country", "test");
$sql->execute();
echo $sql->lastInsertId(); // id of last inserted

I tried, but am receiving an error call to undefined method PDO::lastInsertId()

like image 592
Karem Avatar asked Dec 06 '10 10:12

Karem


Video Answer


1 Answers

I think this can help you:

PDOStatement::rowCount()

returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object. http://php.net/manual/en/pdostatement.rowcount.php

like image 86
EmRa228 Avatar answered Sep 23 '22 02:09

EmRa228