tags:

views:

33

answers:

1

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);
+2  A: 

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.

Tim Chaves
what is $feilds and $i referring to?
Karl Lenz
$fields is simply a resource with table information, used to create the $columns array. $i is an integer counter.
Tim Chaves