stormx Posted December 10, 2008 Share Posted December 10, 2008 Hi guys, the issue with my PHP script at the moment is I want it to die if the file cannot be found in the database, this is my current code: <?php $file = $_GET["id"]; $delid = $_GET["delid"]; include "my_sql.php"; mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die; $myFile = "./images/$file"; unlink($myFile); ?> I put 'or die' after the delete function, but this is totally being ignored and the unlink function is being executed. Please help I want the script to stop if it cannot find anything in the database. Quote Link to comment https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/ Share on other sites More sharing options...
bluesoul Posted December 10, 2008 Share Posted December 10, 2008 Hi guys, the issue with my PHP script at the moment is I want it to die if the file cannot be found in the database, this is my current code: <?php $file = $_GET["id"]; $delid = $_GET["delid"]; include "my_sql.php"; mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die; $myFile = "./images/$file"; unlink($myFile); ?> I put 'or die' after the delete function, but this is totally being ignored and the unlink function is being executed. Please help I want the script to stop if it cannot find anything in the database. The problem is that's going to be valid SQL even if 0 rows are affected so the die is doing what it's supposed to do. Why not check for the existence of the file prior to running the query? Quote Link to comment https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/#findComment-711443 Share on other sites More sharing options...
stormx Posted December 10, 2008 Author Share Posted December 10, 2008 Hi guys, the issue with my PHP script at the moment is I want it to die if the file cannot be found in the database, this is my current code: <?php $file = $_GET["id"]; $delid = $_GET["delid"]; include "my_sql.php"; mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die; $myFile = "./images/$file"; unlink($myFile); ?> Thanks, I will let everyone know how it goes I put 'or die' after the delete function, but this is totally being ignored and the unlink function is being executed. Please help I want the script to stop if it cannot find anything in the database. The problem is that's going to be valid SQL even if 0 rows are affected so the die is doing what it's supposed to do. Why not check for the existence of the file prior to running the query? Quote Link to comment https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/#findComment-711445 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 die() will only work if there was a problem executing the query. If nothing was found in the db, it returns false, but that's not a failed execute. You need to wrap a condition around your unlink. something like this: <?php $file = $_GET["id"]; $delid = $_GET["delid"]; include "my_sql.php"; mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die; ijf (mysql_affected_rows() > 0) { $myFile = "./images/$file"; unlink($myFile); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/#findComment-711450 Share on other sites More sharing options...
stormx Posted December 10, 2008 Author Share Posted December 10, 2008 Figured it out myself, thanks guys heaps <?php $file = $_GET["id"]; $delid = $_GET["delid"]; include "my_sql.php"; $result = mysql_query("SELECT * FROM uploadedimg WHERE name='$file' AND killcode='$delid'"); while($row = mysql_fetch_array($result)) { mysql_query ("DELETE FROM uploadedimg WHERE `name` = '$file' AND `killcode` = '$delid' LIMIT 1")or die; $myFile = "./images/$file"; unlink($myFile); echo "Deleted!"; } echo "Failed"; ?> Happy Holidays Quote Link to comment https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/#findComment-711463 Share on other sites More sharing options...
bluesoul Posted December 10, 2008 Share Posted December 10, 2008 Figured it out myself, thanks guys heaps <?php $file = $_GET["id"]; $delid = $_GET["delid"]; include "my_sql.php"; $result = mysql_query("SELECT * FROM uploadedimg WHERE name='$file' AND killcode='$delid'"); while($row = mysql_fetch_array($result)) { mysql_query ("DELETE FROM uploadedimg WHERE `name` = '$file' AND `killcode` = '$delid' LIMIT 1")or die; $myFile = "./images/$file"; unlink($myFile); echo "Deleted!"; } echo "Failed"; ?> Happy Holidays I would wager that code will echo both Deleted and Failed without a conditional in there somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/#findComment-711468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.