0

I have a basic update query, I want to echo if it was successful so I've tried variations on this

$sql = 'UPDATE storeroom
    SET fruit=?
    WHERE stock_id =?
    AND stock_status <> \'supplied\'';

$stmt = $dbh->prepare($sql);
$stmt->execute($v);

$count = $stmt->rowCount();
}

if ($count < 1) {
echo 'Unknown Stock ID or status change failed.';
}
else {
echo 'Status change successful.';
}

But it fails on the pg_affected_rows expecting a resource.

First time I'm using pg_affected rows which I found here

If I substitute the affected rows for rowcount I get a different error

Kilisi
  • 402
  • 11
  • 33

2 Answers2

1

You may also try an alternative of rowCount - the SQL statement itself returns the number of updated rows. Here is the SQL:

with t as
(
 UPDATE storeroom
    SET fruit = ?
    WHERE stock_id = ?
    AND stock_status <> 'supplied' 
    RETURNING stock_id 
) select count(*) from t;

and

$sql = <<<SQL
with t as
(
 UPDATE storeroom
    SET fruit = ?
    WHERE stock_id = ?
    AND stock_status <> 'supplied'
    RETURNING stock_id 
) select count(*) from t;
SQL;
$stmt = $dbh -> prepare($sql);
$stmt -> execute($v);
$count = $stmt-> fetchColumn();
...
Stefanov.sm
  • 11,215
  • 2
  • 21
  • 21
-2

I think this will do the trick, can depend on PHP version which I am not aware of:

$affected = $stmt->affected_rows;
Peter Koltai
  • 8,296
  • 2
  • 10
  • 20