Page 1 of 1

[Solved] Using PHP to check a MySQL field exists

Posted: Mon Nov 28, 2005 7:06 am
by Grim...
Wasn't sure whether I should have put this here or in Databases, but hey ho.

I want to use PHP to see if a field exists in a table (and if it doesn't, I'll add it).

I've got

Code: Select all

$row = mysql_fetch_row("SELECT count(field_name) from user");
if (!$row)
{
    $sql = "ALTER TABLE user ADD field_name TEXT NOT NULL";
}
But that doesn't work :(

Posted: Mon Nov 28, 2005 7:11 am
by Weirdan

Posted: Mon Nov 28, 2005 7:13 am
by Zoxive

Code: Select all

$sql="SHOW COLUMNS FROM `Table`";
-NSF

Yay!

Posted: Mon Nov 28, 2005 7:21 am
by Grim...
Hurrah!
Thanks chaps!