For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > January 2005 > statement to check whether a field exists









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author statement to check whether a field exists
Phil Da Lick!

2005-01-20, 3:57 pm

Hi,

in php any way to check whether a field exists on a mysql table?

THANKS.
NC

2005-01-20, 3:57 pm

Phil Da Lick! wrote:
>
> any way to check whether a field exists on a mysql table?


Sure:

$fieldname = 'somefield';
$table = 'the_table'
$fieldexists = false;
$result = mysql_query("SHOW FIELDS FROM `$table`");
while ($record = mysql_fetch_array($result)) {
if (strtolower($record['field']) == $fieldname) {
$fieldexists = true;
foreach ($record as $key=>$value) {
echo "$key: $value \r\n";
}
break;
}
}
if (!$fieldexists) {
echo "Field $fieldname does not exist in table $table.";
}

Cheers,
NC

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com