enkidu72 Posted January 23, 2009 Share Posted January 23, 2009 Hi all , I'm stuck with a while that doesn't seems to work ... here the code : $amber_dir_suffix='_amber'; $amber_dir_new=$amber_dir.$amber_dir_suffix; $count=0; $underscore="-"; $query="select id from jobs where workdir='$amber_dir_new' and owner='$user_id'"; while (mysql_numrows(mysql_query($query))) { $count++; $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count; } $result=mkdir("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/"); if($result){ echo "Directory $amber_dir_new created succesfully "; }else{ echo "Cannot create dir $home_dir/$user_dir/Xplor-SC/$amber_dir_new!"; } Basically i need to check if the record exist , and if does increment count ... I'm sure It must be a stupid thing ... Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/ Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 that isn't the proper use of the mysql_* functions...what are you trying to do with that while loop? Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744282 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 I'm not 100% sure what you're trying to do, but the following made sense; <?php $amber_dir_suffix = '_amber'; $amber_dir_new = $amber_dir . $amber_dir_suffix; $count = 0; $underscore = "-"; $query = "SELECT id FROM `jobs` WHERE `workdir`='$amber_dir_new' AND `owner`='$user_id'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_fetch_assoc($result)) { $count++; $amber_dir_new = $amber_dir . $amber_dir_suffix . $underscore . $count; } $dir_result = mkdir("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/"); if($dir_result) { echo "Directory $amber_dir_new created succesfully "; } else { echo "Cannot create dir $home_dir/$user_dir/Xplor-SC/$amber_dir_new!"; } Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744288 Share on other sites More sharing options...
enkidu72 Posted January 23, 2009 Author Share Posted January 23, 2009 I need to check if the record exists in the db ... ( a directory ) If it does , increment count by one . So : count = 0 1) check for "dir" -> dir exists 2) count = 1 check for dir1 -> dir1 exists 3)count=2 check for dir2 -> dir2 doesn't exist create dir dir2 ... Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744293 Share on other sites More sharing options...
enkidu72 Posted January 23, 2009 Author Share Posted January 23, 2009 This was how was working before , now all infos have been saved in a db ... while (file_exists("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/") ){ $count++; $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count; } I just need to do the same querying mysql ... Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744302 Share on other sites More sharing options...
Mark Baker Posted January 23, 2009 Share Posted January 23, 2009 I need to check if the record exists in the db ... ( a directory )A directory? or a db? $count = 0; $query = "SELECT id FROM `jobs` WHERE `workdir`='$amber_dir_new' AND `owner`='$user_id' LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); $count += mysql_num_rows($result); Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744316 Share on other sites More sharing options...
enkidu72 Posted January 23, 2009 Author Share Posted January 23, 2009 the record is a directory name , sorry Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744331 Share on other sites More sharing options...
Psycho Posted January 23, 2009 Share Posted January 23, 2009 The problem is pretty obvious while (mysql_numrows(mysql_query($query))) { $count++; $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count; } Aside from the fact that it should be mysql_num_rows(), the condition never changes, so the loop will continue indefinitely. $amber_dir_new is the only thing changed within the loop, but it is not part of the condition, $query is. If $query was changing records in the db it might be used to exit the loops, but it is only doing a select. In this instance you would need to modify $query to have the loop exit. while (mysql_num_rows(mysql_query($query))) { $count++; $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count; $query="select id from jobs where workdir='$amber_dir_new' and owner='$user_id'"; } But, there are better ways to handle this than running multiple queries. Try this: $amber_dir_suffix = '_amber'; $amber_dir_new = $amber_dir.$amber_dir_suffix; $query="SELECT workdir FROM jobs WHERE workdir LIKE '$amber_dir_new%' AND owner='$user_id'"; $result - mysql_query($query) or die (mysql_error()); $directories = array(); while ($record = mysql_fetch_assoc($result)) { $directories[] = $record['workdir']; } if (in_array($amber_dir_new, $directories)) { $i = 0; do { $amber_dir_new = "{$amber_dir}{$amber_dir_suffix}_{$i}"; $i++; } while (in_array($amber_dir_new, $directories)); } $full_path = "{$home_dir}/{$user_dir}/Xplor-SC/{$amber_dir_new}/"; if(!mkdir($full_path)) { echo "Directory $amber_dir_new created succesfully "; } else { echo "Cannot create dir $home_dir/$user_dir/Xplor-SC/$amber_dir_new!"; } Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744362 Share on other sites More sharing options...
enkidu72 Posted January 23, 2009 Author Share Posted January 23, 2009 MANY THX ! Still doesn't work , but at least I've understood which was my mistake ! Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744396 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 I'm not 100% sure what you're trying to do, but the following made sense; <?php $amber_dir_suffix = '_amber'; $amber_dir_new = $amber_dir . $amber_dir_suffix; $count = 0; $underscore = "-"; $query = "SELECT id FROM `jobs` WHERE `workdir`='$amber_dir_new' AND `owner`='$user_id'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_fetch_assoc($result)) { $count++; $amber_dir_new = $amber_dir . $amber_dir_suffix . $underscore . $count; } $dir_result = mkdir("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/"); if($dir_result) { echo "Directory $amber_dir_new created succesfully "; } else { echo "Cannot create dir $home_dir/$user_dir/Xplor-SC/$amber_dir_new!"; } I made the fixes in the code I posted earlier Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744410 Share on other sites More sharing options...
Psycho Posted January 23, 2009 Share Posted January 23, 2009 MANY thanks ! Still doesn't work , but at least I've understood which was my mistake ! Can you state what problems there are? I wrote my code off the cuff and was not able to test even for syntax errors. But the logic should be sound. I did notice that I had the wrong check on the last IF statement. Should have been if(mkdir($full_path)) Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-744631 Share on other sites More sharing options...
enkidu72 Posted January 24, 2009 Author Share Posted January 24, 2009 Yes , I had already noticed if(mkdir($full_path)) Basically the problem is that if the dir somedir_amber_0 already exists , doesn't create somedir_amber_1 ... Here the output : Array ( [0] => 22-01-09_18-17-03 [1] => 22-01-09_18-17-03_amber [2] => 22-01-09_18-17-03_amber_0 ) SELECT workdir FROM jobs WHERE workdir LIKE '%22-01-09_18-17-03%' AND owner='27' Warning: mkdir() [function.mkdir]: File exists in /var/www/html/stable/xplor_sc.php on line 305 so I think the problem is in the if (in_array($amber_dir_new, $directories)) { $i = 0; do { $amber_dir_new = "{$amber_dir}{$amber_dir_suffix}_{$i}"; $i++; } while (in_array($amber_dir_new, $directories)); } part of the code ... Link to comment https://forums.phpfreaks.com/topic/142112-stuck-in-a-loop/#findComment-745256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.