I have recently written a script that writes to a database every ten minutes. The aim of the script is to update the record if a record for that user exists else it inserts a record for that user.
The database has three important fields, id username ip.
As the IP changes the script will update the IP field for that user or add a record for the user with the new IP and on the next run update the IP rather than add a record.
I am trying to find out the best way to know if the record exists so that I can update rather than insert.
I was previously using $f->rowCount() after the update statement ie
if($f->rowCount() > 0){ continue; }else{ the insert statement}
So if the affected rows were greater than 0 I know the record exists. However, if the IP had not changed, effectively no update ever took place and the $f->rowCount() would be equal to 0.
Now I am simply select all records and store them in an array, check if the username is in_array if so do an update, else do an insert. Is there a better or quicker way to ensure whether a record exists, without having to do a select?
My question is really to see if something similar to $f->rowCount() exists as I only discovered this the other day. Many thanks