B34ST Posted April 18, 2009 Share Posted April 18, 2009 Hi I am trying to update a table if an entry already exists or insert a new entry if it does not. I have the following: $sql = $conn->query("UPDATE rs_stats SET rs_rank='$stats[0]', level='$stats[1]', experience='$stats[2]' WHERE username='$user' AND skill='$skill[$i]'"); if ($conn->affected_rows == 0) { $sql = $conn->query("INSERT INTO rs_stats (username, skill, rs_rank, level, experience) VALUES('$user', '$skill[$i]', '$stats[0]', '$stats[1]', '$stats[2]')"); } The above works fine unless the data stays the same. I think this is because $mysqli->affected_rows only returns a 1 if the row was actually changed but if the row exists and all data is the same it returns a 0. I would like to know how to find out how many matches there where instead? If i use mysqli->info it returns this and i could use preg_match to see if it a 1 or a 0 but this seems to be a long way of doing it. any ideas? any help appreciated Thanks Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/ Share on other sites More sharing options...
soak Posted April 18, 2009 Share Posted April 18, 2009 You could use INSERT / ON DUPLICATE KEY UPDATE Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/#findComment-813367 Share on other sites More sharing options...
B34ST Posted April 18, 2009 Author Share Posted April 18, 2009 I tried using on duplicate key but again it does not achieve what i need as it only works on a primary or unique key. I cannot use the primary key as this an auto_increment value. and I cannot set the field to be unique because there will be multiple entries the same for example for each user there will be 29 different skills so i must use both username and skill in the mysql WHERE statement. here is an example of the table: id | username | skill | rank | level |experience 1 | user1 | skill1 | 1 | 99 | 200000 2 | user1 | skill2 | 3 | 93 | 100000 3 | user2 | skill1 | 5 | 94 | 50000 4 | user2 | skill2 | 7 | 98 | 20000 Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/#findComment-813455 Share on other sites More sharing options...
soak Posted April 18, 2009 Share Posted April 18, 2009 Just put a unique index on username AND skill. That way your data is protected at the DB level too. Or you could cheat by adding a lastupdated column to the table so that the table is always updated and your original example works. Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/#findComment-813465 Share on other sites More sharing options...
B34ST Posted April 18, 2009 Author Share Posted April 18, 2009 The table will not let me put a unique key on username and skill because the username should be inserted on 29 rows. however adding a last_updated column isnt a bad idea, this could help with identifying non active members. thanks for the help. I will leave this topic as unsolved for now although this "cheat" will solve my problem it would still be nice to know if there is another way around this. Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/#findComment-813467 Share on other sites More sharing options...
soak Posted April 18, 2009 Share Posted April 18, 2009 Not sure if you've tried but you can add one unique index to two columns at once, not username and skill separately: ALTER TABLE `rs_stats` ADD UNIQUE ( `username` , `skill` ) Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/#findComment-813472 Share on other sites More sharing options...
B34ST Posted April 19, 2009 Author Share Posted April 19, 2009 Ah now I see, No I did not know that I could add two unique keys at the same time. When I tried to do them seperately I was given an error but now trying it your way has worked and now so does the on duplicate key statement. Thanks for all the help. Link to comment https://forums.phpfreaks.com/topic/154673-solved-mysqli-affected_rows-help-needed/#findComment-813704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.