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 Quote Link to comment 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) Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Mad Mick Posted December 15, 2008 Share Posted December 15, 2008 Fair enough, much neater!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.