pneudralics Posted September 20, 2009 Share Posted September 20, 2009 I want to create a random string. When the random string is created it's inserted into the database. I don't know how to loop the statement if the data is already in the database. if () { //simulating random string created } //Simulating search database for created string if (created random string is in the database) { //start back to simulating random string created and repeat until new string is created } else { //post new string in database } Link to comment https://forums.phpfreaks.com/topic/174904-how-can-i-loop-back-to-data-creation-if-the-data-is-already-in-the-database/ Share on other sites More sharing options...
abazoskib Posted September 20, 2009 Share Posted September 20, 2009 if you are using mysql, and the random string is a key, you can use INSERT IGNORE or INSERT...ON DUPLICATE UPDATE queries. if you want to do it through php/you want to use standard sql, create the random string. then try to select the string from the table you are inserting from, i.e. SELECT * FROM col WHERE str='$myStr' If this query returns zero rows, insert the new string. if it exists, do nothing, and continue. put this all in one loop. Link to comment https://forums.phpfreaks.com/topic/174904-how-can-i-loop-back-to-data-creation-if-the-data-is-already-in-the-database/#findComment-921776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.