4

I have the following problem,I don't know how to check if my MySql query has actually inserted or ignored the insert from my php code.
This is because as far as I know it always returns true, due to the error preventing of the ignore clause.
A sample of what I have is:

$addTagSql = "INSERT IGNORE INTO tags (text) 
  VALUES ('$text')";
$resultAddTag = $db -> query($addTagSql);

if(!$resultAddTag) {
  $response["success"] = 0;
}
else{
  $response["success"] = 1;
}  

Should I just let it break and handle the error?
I couldn't find a good answer, so I'd be glad if you can help me :)

Asur
  • 379
  • 3
  • 20
  • You can use `ROW_COUNT()`: http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_row-count. – Gordon Linoff Feb 23 '16 at 12:03
  • @GordonLinoff Is that reliable? Imagine that I have several accesses deleting, inserting and updating at the same time, I think that could mess it up at some point... – Asur Feb 23 '16 at 12:05

3 Answers3

5

Use mysqli_affected_rows()

reference: http://php.net/manual/en/mysqli.affected-rows.php

B-and-P
  • 1,693
  • 10
  • 26
3

You can try with $mysqli->affected_rows

hsz
  • 148,279
  • 62
  • 259
  • 315
2

insert ignore used to ignore the error, you can use insert and check if it return sql error or not.

Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38