I use the below query to add/insert a record only if an existing record with a certain value does not already exist:
php:
$query = "INSERT INTO accounts_mm (abc, name)
SELECT '".$mysqli->real_escape_string($abc)."', '".$mysqli->real_escape_string($name)."' FROM DUAL
WHERE NOT EXISTS (
SELECT abc FROM accounts_mm WHERE abc = '".$mysqli->real_escape_string($abc)."'
)";
... it works ... but I always get 'Success' returned also in cases where no records were inserted.
if($mysqli->query($query)){
echo json_encode(array('added' => 'Success!'));
} else {
echo json_encode(array('added' => 'Failure!'));
}
How can I check on success and failure for the above query?
Further attempts:
if(mysqli_affected_rows($mysqli->query($query)) > 0){
echo json_encode(array('added' => 'Success!'));
} else {
echo json_encode(array('added' => 'Failure!'));
}