JaredRitchey Posted May 30, 2011 Share Posted May 30, 2011 I have a script that runs at the end of a batch process specifically built to clean out empty records. How can I make it a function and then call the function. Here is my query; $sql="DELETE FROM ".$dbname." . property WHERE property . id=''"; mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); $sql="DELETE FROM ".$dbname." . agents WHERE agents . ip_source=''"; mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); $sql="DELETE FROM ".$dbname." . settings WHERE settings . prop_id=''"; mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); I would like this to be in a function that i could specifically call the function. Why? Well because I have 5 different sets of queries like this set above and I'm going to build a conditional. So like if condition 1 then run use this function, condition 2, use function ??? etc and so on. Link to comment https://forums.phpfreaks.com/topic/237889-query-function-help/ Share on other sites More sharing options...
wildteen88 Posted May 30, 2011 Share Posted May 30, 2011 specifically built to clean out empty records. If you're validating your data correctly before it is inserted into the database then you shouldn't need to do this. If you want to make your code a function then do this function deleteEmptyRecords($dbname) { $sql="DELETE FROM ".$dbname." . property WHERE property . id=''"; mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); $sql="DELETE FROM ".$dbname." . agents WHERE agents . ip_source=''"; mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); $sql="DELETE FROM ".$dbname." . settings WHERE settings . prop_id=''"; mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); } Call it using deleteEmptyRecords($dbname); Link to comment https://forums.phpfreaks.com/topic/237889-query-function-help/#findComment-1222429 Share on other sites More sharing options...
JaredRitchey Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks sincerely, but will that work even though $dbname is a dynamic variable? Link to comment https://forums.phpfreaks.com/topic/237889-query-function-help/#findComment-1222434 Share on other sites More sharing options...
.josh Posted May 30, 2011 Share Posted May 30, 2011 You are supposed to pass the $dbname value to the function, when you call the function. Link to comment https://forums.phpfreaks.com/topic/237889-query-function-help/#findComment-1222435 Share on other sites More sharing options...
wildteen88 Posted May 30, 2011 Share Posted May 30, 2011 Should do as you're passing $dbname when it is being called deleteEmptyRecords($dbname); Link to comment https://forums.phpfreaks.com/topic/237889-query-function-help/#findComment-1222436 Share on other sites More sharing options...
JaredRitchey Posted May 30, 2011 Author Share Posted May 30, 2011 Okay, that makes sense now, thanks! Link to comment https://forums.phpfreaks.com/topic/237889-query-function-help/#findComment-1222455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.