Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

Securing a LAMP Server with mod_security
osCommerce Easypopulate File Creator Class
CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

MySQL Add Column if Not Exists - PHP Function

March 13th, 2008
Read More mysql, php

This is a nice little function which I struggled to find elsewhere on the web.

As discussed elsewhere this is not the kind of thing that should be included in a public facing script, but for administration tools etc its pretty handy:

PLAIN TEXT
PHP:
  1. function add_column_if_not_exist($db, $column, $column_attr = "VARCHAR( 255 ) NULL" ){
  2.     $exists = false;
  3.     $columns = mysql_query("show columns from $db");
  4.     while($c = mysql_fetch_assoc($columns)){
  5.         if($c['Field'] == $column){
  6.             $exists = true
  7.             break;
  8.         }
  9.     }      
  10.     if(!$exists){
  11.         mysql_query("ALTER TABLE `$db` ADD `$column`  $column_attr");
  12.     }
  13. }

This function grabs the column information for the table, then it loops through the info looking for the column. If it finds the column then it acknowledges that it exists and ceases the loop.

After this stage, the function checks to see if exists is true or not, and if it is not true then it adds the column with the attributes defined when calling the function.

The default attributes are for a VARCHAR field up to 255 characters in length and NULL enabled.

Bookmark this Post
Add 'MySQL Add Column if Not Exists - PHP Function' to Del.icio.usAdd 'MySQL Add Column if Not Exists - PHP Function' to diggAdd 'MySQL Add Column if Not Exists - PHP Function' to FURLAdd 'MySQL Add Column if Not Exists - PHP Function' to blinklistAdd 'MySQL Add Column if Not Exists - PHP Function' to redditAdd 'MySQL Add Column if Not Exists - PHP Function' to Feed Me LinksAdd 'MySQL Add Column if Not Exists - PHP Function' to TechnoratiAdd 'MySQL Add Column if Not Exists - PHP Function' to Yahoo My WebAdd 'MySQL Add Column if Not Exists - PHP Function' to NewsvineAdd 'MySQL Add Column if Not Exists - PHP Function' to SocializerAdd 'MySQL Add Column if Not Exists - PHP Function' to Ma.gnoliaAdd 'MySQL Add Column if Not Exists - PHP Function' to Stumble UponAdd 'MySQL Add Column if Not Exists - PHP Function' to Google BookmarksAdd 'MySQL Add Column if Not Exists - PHP Function' to RawSugarAdd 'MySQL Add Column if Not Exists - PHP Function' to SquidooAdd 'MySQL Add Column if Not Exists - PHP Function' to SpurlAdd 'MySQL Add Column if Not Exists - PHP Function' to BlinkBitsAdd 'MySQL Add Column if Not Exists - PHP Function' to NetvouzAdd 'MySQL Add Column if Not Exists - PHP Function' to RojoAdd 'MySQL Add Column if Not Exists - PHP Function' to BlogmarksAdd 'MySQL Add Column if Not Exists - PHP Function' to ShadowsAdd 'MySQL Add Column if Not Exists - PHP Function' to Co.mments
Add 'MySQL Add Column if Not Exists - PHP Function' to ScuttleAdd 'MySQL Add Column if Not Exists - PHP Function' to BloglinesAdd 'MySQL Add Column if Not Exists - PHP Function' to TailrankAdd 'MySQL Add Column if Not Exists - PHP Function' to SegnaloAdd 'MySQL Add Column if Not Exists - PHP Function' to OKnotizieAdd 'MySQL Add Column if Not Exists - PHP Function' to NetscapeAdd 'MySQL Add Column if Not Exists - PHP Function' to Bookmark.itAdd 'MySQL Add Column if Not Exists - PHP Function' to AskAdd 'MySQL Add Column if Not Exists - PHP Function' to SmarkingAdd 'MySQL Add Column if Not Exists - PHP Function' to LinkagogoAdd 'MySQL Add Column if Not Exists - PHP Function' to DeliriousAdd 'MySQL Add Column if Not Exists - PHP Function' to SocialdustAdd 'MySQL Add Column if Not Exists - PHP Function' to Live-MSNAdd 'MySQL Add Column if Not Exists - PHP Function' to SlashDotAdd 'MySQL Add Column if Not Exists - PHP Function' to SphinnAdd 'MySQL Add Column if Not Exists - PHP Function' to DiggitaAdd 'MySQL Add Column if Not Exists - PHP Function' to SeotribuAdd 'MySQL Add Column if Not Exists - PHP Function' to FaceBookAdd 'MySQL Add Column if Not Exists - PHP Function' to UpnewsAdd 'MySQL Add Column if Not Exists - PHP Function' to WikioAdd 'MySQL Add Column if Not Exists - PHP Function' to Social Bookmarking Reloaded

Feed | Respond | Trackback

2 Responses to “MySQL Add Column if Not Exists - PHP Function”

  1. Tom Haigh Says:
    May 30th, 2008 at 2:14 pm

    You could also do:

    if (false === mysql_query("select colname from tablename limit 0")) {
    //add the column
    }

  2. admin Says:
    May 30th, 2008 at 2:28 pm

    would this not give a false negative if the table was empty?

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • security
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • File Comparison on Ubuntu
    • 10 SEO Myths
    • PHP: Recursive Create Path (if not exists)
    • HOSTS file, Vista and Blocking Unwanted Adverts
    • Populating osCommerce and other Ecommerce Platforms
    • Favourite Firefox Extensions
    • Essential Search Engine Optimisation Research Tool
    • Magento UK
    • Purashop : SEO Services
    • Plesk Web Fusion PHP Limits

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design