wiggst3r Posted December 15, 2008 Share Posted December 15, 2008 Hi I'm, trying to generate a string that has 7 chars. The first one must be 'f' and the remaining 6 have to be all uppercase alphanumeric and must not contain '0' (zero), i or O. The script is to update a current row in a mysql database, which there is 150 rows. Each string must be unique, so I need some way of checking for the string in before I update each row. Can anyone help me? Thanks Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/ Share on other sites More sharing options...
Mchl Posted December 15, 2008 Share Posted December 15, 2008 Create UNIQUE index on this field, so that MySQL will not allow you to insert same code twice (the query will return errorno 1062) Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/#findComment-715897 Share on other sites More sharing options...
wiggst3r Posted December 15, 2008 Author Share Posted December 15, 2008 Is there anyway in PHP that I can do this? That will check if the string isn't already there, generate another string update the row and then move onto the next? Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/#findComment-715900 Share on other sites More sharing options...
Mchl Posted December 15, 2008 Share Posted December 15, 2008 Yes... you could do a SELECT query first to check if it already exists in table, but using UNIQUE index is much better IMHO Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/#findComment-715902 Share on other sites More sharing options...
Mad Mick Posted December 15, 2008 Share Posted December 15, 2008 I would generate an array of strings - you can check for a repeated string each time you generate a new one using in_array(). Then you can chuck the whole array into your MySQL table when you have finished. I would think it would be easier than handling MySQL errors. Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/#findComment-715903 Share on other sites More sharing options...
Mchl Posted December 15, 2008 Share Posted December 15, 2008 do { $uniqueString = generateString(); mysql_query("INSERT INTO table VALUES ('$uniqueString')"); } while (mysql_errno() == 1062) What's easier than this? [edit] That'd be == 1062 not != 1062 Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/#findComment-715907 Share on other sites More sharing options...
Mad Mick Posted December 15, 2008 Share Posted December 15, 2008 Fair enough, much neater!! Link to comment https://forums.phpfreaks.com/topic/137074-string-checking-and-updating-mysql/#findComment-715955 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.