Function Needed

I need a function that will delete records from X number of tables with a common uniqud id.

For instance, I have 4 tables -  photos, items, stats, equipment

each of them has a field called equipment_id (which is the primary key).

Now, I want to have one function where I pass the id and it deletes all in a table with that id. Those table names are just a sample, in the function I need to be able to pass the names.

Please remember, the Number of tables I will pass to the function will vary.

Thanks!!
LVL 1
edvinsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi, make sure that you have mysql connection before running the function:


function deletefromtables($id)
{
    $args = func_get_args();
 
    for($i = 1; $i < func_num_args();$i++)
    {
        $sql = "DELETE FROM `$args[$i]` WHERE `equipment_id` = $id";
        mysql_query($sql) or die(mysql_error());
    }
}


Call it as ..

deletefromtables(123,"photos","items","stats","equipment");

---
Harish
TeRReFCommented:
I think mgh_mgharish's solution is very good, but if you insist on only passing the primary key to your function, it becomes more complicated, but doable.
I've written a similar function for a client (It is copyrighted code, so I can't give it to you unfortunately).
What I did was
A show tables query and use that result to loop through al the tables with a describe query. Use that result to check if primary key exists and perform the appropriate action on that table.

Hope this will help
edvinsonAuthor Commented:
Very close to what I need, mgh_mgharish .

And thanks fot the tips Terref.

Here's how I would *like* to call the function:

deletefromtables("myuniqueidFieldNameHere",myUniqueValueHere,"photos","items","stats","equipment");
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

mensuckCommented:
<?

function delete_items ()
{
      $args = func_get_args();

      $sql = "DELETE FROM " . implode ( ', ', array_splice ( $args, 3 ) ) . " WHERE " . $args[1] . " = " . $args[2];

      mysql_query ( $sql, $args[0] );

      echo $sql . $args[0];
}


delete_items ( 'db_resource_handle', 'column_name_to_delete_from', 'column_value_to_delete_on', 'table_1', 'table_2', 'table_3', 'more_tables' );

?>


ms!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mensuckCommented:
The echo at the bottom is just so you could see the result, just remove it in the real function!\

ms!
edvinsonAuthor Commented:
sweet. thanks!

I already used mgh_mgharish's solution in our project ( by figuring out the answer myself ) but s/he never responded to my last post.

I will keep your solution also!

Mind if I split the points?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.