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?
Test if table field exists..
Moderator: General Moderators
-
jamiel
- Forum Contributor
- Posts: 276
- Joined: Wed Feb 22, 2006 5:17 am
- Location: London, United Kingdom
You could do this ...
Better ways to write it but you get the idea.
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;
}
}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;
}
}