0

The following will echo $response of 1 if there is no error regardless whether a record was inserted.

I read PDO mysql: How to know if insert was successful, however, it just describes if the query was unsuccessful presumably doe to an error.

How can I confirm whether a record was inserted or not?

<?php
$sql='INSERT INTO t3(id,data) SELECT id,data FROM t2 WHERE id=?';
$stmt=db::db()->prepare($sql);
$response=$stmt->execute(array(123));
echo($response);
?>
Community
  • 1
  • 1
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • 1
    Unless you've set a different error mode, PDO will simply return boolean false on error, or a 1/handle/object on success. Since you're doing an insert, there's no result set, so you'll get a `1` (aka true) back to indicate success. You can also use [rowCount](http://php.net/manual/en/pdostatement.rowcount.php) – Marc B Jan 07 '15 at 19:13
  • @MarcB `rowCount` would work. Didn't know if this was my only optino. – user1032531 Jan 07 '15 at 19:14

2 Answers2

0

PDO::rowCount will return the number of affected rows.

So you could: echo $stmt->rowCount();

Dan
  • 10,614
  • 5
  • 24
  • 35
0

maybe an awful answer but what about to create a select to find your insert and then simply compare both your insert and your select, or a select with a count with the variables of your input?

Other option may be to check the last inserted ID

Community
  • 1
  • 1
Mark
  • 684
  • 2
  • 9
  • 25