Jump to content

How can I loop back to data creation if the data is already in the database?


pneudralics

Recommended Posts

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
}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.