Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if INSERT IGNORE has actually inserted the row from php [duplicate]

Tags:

php

mysql

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 :)

like image 337
Asur Avatar asked Jan 06 '23 14:01

Asur


1 Answers

Use mysqli_affected_rows()

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

like image 133
B-and-P Avatar answered Jan 16 '23 20:01

B-and-P