Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO - check if row was updated?

Tags:

php

mysql

pdo

Other people have asked this question, but mine is a little more specific.

I have this query:

$sql = "UPDATE table SET option=? WHERE number=?";
$q = $conn->prepare($sql);
$q->execute(array($option, $number));
$q->setFetchMode(PDO::FETCH_BOTH);

echo $q->rowCount();

If the WHERE number already exists and the SET option is same, $q->rowCount() equals 0

If the WHERE number doesnt exist and the row does not update, $q->rowCount() equals 0

How can I distinguish between these non-updates?

like image 220
supercoolville Avatar asked Jan 26 '26 07:01

supercoolville


1 Answers

On recent PHP versions, it's controlled by the PDO::MYSQL_ATTR_FOUND_ROWS attribute. When set to true, according to the doc, the effect is:

Return the number of found (matched) rows, not the number of changed rows.
like image 102
Daniel Vérité Avatar answered Jan 28 '26 21:01

Daniel Vérité