0

I have an array of all the tables in my database and I need to go in a delete the record if the applicationid=123 or familyid=123. But some tables dont have any of those feilds is there a way I can just skip the table if that is the case? instead of it erroring.

$query2="DELETE FROM "$table[$x]" WHERE applicationid = '517f20805d9bd0961c76e8f544ee6f76' OR house1ID = "$r['1']" OR house2ID ="$r['2']" OR familyID="$r['1']" OR familyID="$r['2']"";
        $result2 = mysql_db_query($aidDB, $query2, $connection);
Benjamin
  • 551
  • 5
  • 25
Karl Lenz
  • 35
  • 5
  • possible duplicate of [DELETING any record in any table in the database if feildx=123](http://stackoverflow.com/questions/3365822/deleting-any-record-in-any-table-in-the-database-if-feildx123) Which has the answer to the exact question being asked in the answers) – ircmaxell Jul 30 '10 at 15:59

1 Answers1

2

I believe this is what you need: http://www.lost-in-code.com/programming/mysql/mysql-check-if-field-exists/

$tableFields = mysql_list_fields("databasename", "tablename");

$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
    $field_array[] = mysql_field_name($fields, $i);
}

Then use in_array() to check if the field exists.

On second thought, you can use SHOW COLUMNS syntax from MySQL in your query:

SHOW COLUMNS FROM tablename;

Iterate through and put them in an array like above and then check for a match. Apparently mysql_list_fields is deprecated.

timmyc
  • 512
  • 1
  • 7
  • 16