[Solved] Using PHP to check a MySQL field exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

[Solved] Using PHP to check a MySQL field exists

Post 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 :(
Last edited by Grim... on Mon Nov 28, 2005 7:22 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

$sql="SHOW COLUMNS FROM `Table`";
-NSF
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Yay!

Post by Grim... »

Hurrah!
Thanks chaps!
Post Reply