Problem changing folder names with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boon4376
Forum Newbie
Posts: 19
Joined: Sun Oct 01, 2006 9:55 pm

Problem changing folder names with PHP

Post by boon4376 »

So when I started making this database a while ago, I did it wrong, I made the primary key the "student id" or SID which after a few thousand records turned out to be inconsistent, So i have developed a new RID "record id" which I aim to replace SID as the new primary key (which is just an auto_increment primary key that i created after all the data was entered).... Right now the folders which contain the records are all named after the students SID, but i need to change the folder names to the new RID... Since there are over 5000 records i want some code to do this..

I'm pretty close but im running into some huge problems when its executed that I cannot explain.

these are my steps
1.) use scandir to load the names of all the folders into an array.
2.) lookup the RID of that record where SID in the database = SID of the folder
3.) change folders name to RID.
4.) change code to lookup records based on RID instead of SID

Now in my code im going as far as just printing "Will change folder from [SID] to [RID]" displaying the actual names, and a line break, which shows the new folder names for all 5000+ records.. I havnt implemented the code to actually change the folder names... yet

When I go to echo the array elements with a while and a counter, all 5000+ are listed fine in order... however when I do the database lookup during the while and say "echo 'will change folder from [SID] to [RID]' <br>"; I get some REALLY funky output...

Code: Select all

$folders = scandir("records/");
 
// Get number of folders
$counted = count($folders);
echo "there are $counted folders <br><br>";
 
$counter = 0;
 
while($counter < $counted){
 
//=================================>
// Get connection to database and
// retreive the RID's of each of
// these folders names. The folders 
// are named after the records SID.
//=================================>
 
//======= Get Connection to DB ========>
 
  $link = functions::getConnection();
 
//=====================================>
$query = "SELECT RID FROM students WHERE SID = " . $folders[$counter];
$result = mysql_query($query);
 
    while ($row_data = mysql_fetch_array($result))
 
    {
      $RID = $row_data['RID'];
      echo "Need to change " . $folders[$counter] . " to " . $RID . " <br>";
 
    }// END INNER WHILE
 
//========= Close Link to DB ==========>
 
  mysql_close($link);
 
//=====================================>
$counter++;
}// END WHILE
So naturally I'd expect the output to show the first SID and the RID rid the folder name will be changed to, then the second SID, and the second RID... etc etc...
as when i normally echo the $folders[$counter] I get a neatly ordered list of all of the folder names....

Here's what my output looks like when i try to execute the code above...
Need to change 000-00-0000 to 1
Need to change 000-00-0000 to 2
Need to change 000-00-0000 to 3
Need to change 000-00-0000 to 4
Need to change 000-00-0000 to 5
Need to change 000-00-0000 to 6
Need to change 000-00-0000 to 7
Need to change 000-00-0000 to 8
Need to change 000-00-0000 to 9
Need to change 000-00-0000 to 10
Need to change 000-00-0000 to 11
Need to change 000-00-0000 to 12
Need to change 000-00-0000 to 13
Need to change 000-00-0000 to 14
Need to change 000-00-0000 to 15
Need to change 000-00-0000 to 5296
Need to change 0041573124 to 424
Need to change 0311151956 to 1052
Need to change 0699009267 to 1238
As you can see, the same SID gets repeated multiple times with different RID's each time, and there is no order to the RID's it starts to jump around a lot.. the SID's repeat multiple times as well.. Keep in mind the first # is the name of the folder and the second number after "to" is what the folder will be renamed to.

if it was working the RID's would number from 1 - 5000+ neatly...

What is going wrong with my code, is it something obvious? Is it a php bug? Is it my computer???
boon4376
Forum Newbie
Posts: 19
Joined: Sun Oct 01, 2006 9:55 pm

Re: Problem changing folder names with PHP

Post by boon4376 »

I solved the problem, because the cell data has a hyphen in the name, I needed to put single quotes around the SID in the query.

this is my new code

Code: Select all

 
$folders = scandir("records/");
 
// Get number of folders
$counted = count($folders);
echo "there are $counted folders <br><br>";
 
$counter = 0;
 
//======= Get Connection to DB ========>
 
  $link = functions::getConnection();
 
//=====================================>
 
while($counter < $counted){
 
//=================================>
// Get connection to database and
// retreive the RID's of each of
// these folders names. The folders 
// are named after the records SID.
//=================================>
 
$query = "SELECT `RID` FROM students WHERE SID = " . "'" . $folders[$counter] . "'";
 
$result = mysql_query($query);
 
    if($row_data = mysql_fetch_array($result))
 
    {
      $RID = $row_data['RID'];
      echo "Will change folder name from " . $folders[$counter] . " to " . $RID . "<br>";
    }// END IF
    else{
      echo $folders[$counter] . " is not a value we cant find in the database<br>";
    }
 
$counter++;
}// END WHILE
 
//========= Close Link to DB ==========>
 
  mysql_close($link);
 
//=====================================>
 
the output has corrected accordingly
Will change folder name from 000-00-0000 to 1
Will change folder name from 000-00-0001 to 2
Will change folder name from 000-00-0002 to 3
Will change folder name from 000-00-0003 to 4
Will change folder name from 000-00-0004 to 5
Will change folder name from 000-00-0005 to 6
Will change folder name from 000-00-0006 to 7
Will change folder name from 000-00-0007 to 8
Will change folder name from 000-00-0009 to 9
Will change folder name from 000-00-0010 to 10
Will change folder name from 000-00-0012 to 11
Will change folder name from 000-00-0013 to 12
This is the code I will use to rename the folders

Code: Select all

 rename($oldname, $newname);
Post Reply