Page 1 of 1
Test if table field exists..
Posted: Fri Jul 14, 2006 3:50 am
by Benjamin
What would be the best way to test a table for a field. I need to check whether or not certain fields exists.
Just run a test query and catch the error?
Would this be something suited for the try and catch functions?
Posted: Fri Jul 14, 2006 3:57 am
by jamiel
You could do this ...
Code: Select all
function colExists($colName)
{
$sql = "DESCRIBE `table_name`;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
if ($colName == $row['Field'])
return true;
}
}
Better ways to write it but you get the idea.
Posted: Fri Jul 14, 2006 4:20 am
by Benjamin
Cool thanks, I used my database class
Code: Select all
$Database->query = "DESCRIBE `thisTable`";
$Database->sendQuery();
while ($Data = $Database->fetchRow) {
if ($_POST['FieldName'] == $Data['Field']) {
// field exists..
break;
}
}