0

Right now I am using Insert Ignore to insert a bunch of tuples into my DB. However, I want to know whether or not the tuple was actually inserted or not and based on that do some action. Code is in PHP:

if(!($stmt4 = $mysqliprivate3->prepare("INSERT IGNORE INTO ".$table." VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))){
                                echo "Prepare Failed3: (" . $mysqliprivate3->errno . ") " . $mysqliprivate3->error;
                            }
                            else{
                                $stmt4->bind_param("ssisssssssssssss", $id, $dt, $score, $question1, $question2, $question3,$question4,$question5,$question6,$question7,$question8,$question9a,$question9b,$question9c,$question9d,$question9e);
                                $stmt4->execute();
                                $stmt4->close();
                            }
                            //if(inserted){
                                Do something
                            }

I have tried using echo $mysqliprivate3->affected_rows; but it seems to give me a 1 even if it isn't inserting anything. What else could I do?

Thanks,

Pompey
  • 616
  • 2
  • 9
  • 23
  • I believe a collision will result in a warning, but issuing and processing a `SHOW WARNINGS` statement would be a pain. (I suspect there's a much better way, but not sure what API you're using, nor am I very familiar with how MySQL handles this.) – Corbin Jul 12 '12 at 23:05

1 Answers1

2

try this:

if ($stmt4->affected_rows)
{
    // ...
}
Federkun
  • 36,084
  • 8
  • 78
  • 90