rickyj Posted July 7, 2007 Share Posted July 7, 2007 anyone got an idea how to complete the following while loop. I want to set a database column (LT_URL) to a sting ($ltURLarray), but 1st i want to check if it already exist in the db, if it does exist, then I want to test the next sting exist in the db, if it doesnt set LT_URL to $ltURLarray on the i'th attempt. $ltURLarray[0] = 'string0'; $ltURLarray[1] = 'string1'; //....... $ltURLarray[100] = 'string100'; // some how put the follwing in a while loop while i<100: i = 0; URLdata = mysql_query("SELECT LT_URL FROM lt_Users WHERE LT_URL = '$ltURLarray[i]' LIMIT 1")or die(mysql_error()); if (!mysql_num_rows($URLdata)){ $ltURL =$ltURLa[0]; } Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/ Share on other sites More sharing options...
Yesideez Posted July 7, 2007 Share Posted July 7, 2007 <?php foreach ($ltURLarray as $ltURL) { $fetch=mysql_fetch_assoc(mysql_query("SELECT `LT_URL` FROM `lt_users` WHERE `LT_URL`='".$ltURL."'")); if (empty($fetch['LT_URL'])) { mysql_query("INSERT INTO `lt_users` (`LT_URL`) VALUES ('".$ltURL."')"); } } ?> Something like that I guess is what you're looking for. Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/#findComment-291900 Share on other sites More sharing options...
rickyj Posted July 7, 2007 Author Share Posted July 7, 2007 Thanks for the quick response Will foreach not go through each one(regardles of result), I want to stop if the 1st is not found in the colum, or if not try second.... Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/#findComment-291908 Share on other sites More sharing options...
Yesideez Posted July 7, 2007 Share Posted July 7, 2007 If one is found and you want to stop the foreach() loop then add this line as part of an else for the if() statement: } else { break; } Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/#findComment-291911 Share on other sites More sharing options...
rickyj Posted July 7, 2007 Author Share Posted July 7, 2007 dow! break... thats what i was looking for, just forgot, thanks Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/#findComment-291913 Share on other sites More sharing options...
rickyj Posted July 7, 2007 Author Share Posted July 7, 2007 I have something else that does the inserting, so the follwing code should work? foreach ($ltURLarray as $ltURL) { $fetch=mysql_fetch_assoc(mysql_query("SELECT `LT_URL` FROM `lt_users` WHERE `LT_URL`='".$ltURL."'")); if (empty($fetch['LT_URL'])) { break; } } // so now $ltURL is set to $ltURLarray (where ltURLarray is not found) (sorry for me being a bit dumb and spelling it out in full), do you think this will work? Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/#findComment-291930 Share on other sites More sharing options...
rickyj Posted July 7, 2007 Author Share Posted July 7, 2007 the above didnt work, it just set it to the first one, regardles of it already being present in the database! but I used: foreach ($ltURLarray as $ltURL) { $fetch=(mysql_query("SELECT `LT_URL` FROM `lt_users` WHERE `LT_URL`='".$ltURL."'")); if (empty($fetch['LT_URL'])) { break; } } why dont these posts merge Link to comment https://forums.phpfreaks.com/topic/58836-anyone-know-how-to-put-this-in-a-while-loop/#findComment-292030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.