PDA

View Full Version : fairly simple mysql query and file delete problem


yongfook
05-08-2005, 11:55 AM
Woohoo, first post! Been a long time unregistered lurker of these forums - this place has solved my coding problems a number of times. But here's one I can't seem to find an answer for...


//find what the oldest record is in the table
$getoldestfile = "SELECT * FROM mytable WHERE id>1 LIMIT 1";
$oldfile=mysql_query($getoldestfile);
$byebye=mysql_result($oldfile, 0, "url");
//and delete the file from the server
unlink("images/$byebye.jpg");

//then get rid of the oldest entry
$deleteoldestrecord = "DELETE FROM mytable WHERE id>1 LIMIT 1";
mysql_query($deleteoldestrecord);


Something is wrong with the above script. I know the queries are working so actually I think it is a problem with my php (so perhaps this should have gone in the php forum? sorry!).

Basically I get the name of a file from the table, then delete it from my server. Everything works apart from the file delete - I think maybe I am using mysql_query and mysql_result incorrectly.

Any help appreciated!
YF

SeeIT Solutions
05-08-2005, 12:21 PM
it is a php question...

Do you have more code than that? do you connect to your database and select it anywhere?

also yes your mysql_result line is wrong.

Will be able to fix this with a little more information unless this works


//replace
$oldfile=mysql_query($getoldestfile);
$byebye=mysql_result($oldfile, 0, "url");
//and delete the file from the server
unlink("images/$byebye.jpg");


//with
if (mysql_num_rows($oldfile) > 0) {
$result = mysql_fetch_assoc($oldfile);
unlink("images/".$result['url'].".jpg");
}

yongfook
05-08-2005, 12:25 PM
nope, no error - it goes through to the end of the script fine - it just doesn't delete that file!

SeeIT Solutions
05-08-2005, 12:28 PM
sorry, edited my post ^^^ read above

yongfook
05-08-2005, 12:32 PM
yes naturally I am connecting to a database and selecting it etc - I just presumed that was a given so I didn't bother to paste up the code. There is more code than this, and the rest of it works fine - it's just this one bit.

I'll try your solution though - many thanks!

Tangerine Dream
05-08-2005, 02:38 PM
yes naturally I am connecting to a database and selecting it etc
Hi, just to be sure: $byebye variable contains file name and your PHP script is able to delete files, i.e


unlink("images/test-file-to-delete.jpg");

deletes the required file?

yongfook
05-08-2005, 02:42 PM
that's right - $byebye is the name of the file apart from the file extension (.jpg).

then I simply unlink the file...it SHOULD delete...but doesn't!

unlink works normally so it's not a permissions problem etc.

Tangerine Dream
05-08-2005, 04:49 PM
then I simply unlink the file...it SHOULD delete...but doesn't!
Since you said that there are no warning or error messages, try to place below code line before unlink():

// report all errors
error_reporting(E_ALL);