Jump to content

Query Function Help


JaredRitchey

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.