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

Zend_Dojo Dijits and Numeric ID’s
Happy New Year from Edmonds Commerce
Easy Security for PHP Scripts
Secure and Insecure Contents in HTTPS

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.

Feed | Respond | Trackback

3 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?

  3. PeerBr Says:
    December 30th, 2008 at 1:45 pm

    Thanks.

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • business
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • edmondscommerce
    • 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
    • seo
    • spidering
    • ubuntu
    • Uncategorized
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css 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 search engine optimisation security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Google Checkout Integration Tools
    • Faster File Browsing with Tabs for Ubuntu
    • MySQL DB Admin GUI for Windows / XAMPP
    • Flash and PHP Charts and Graphs
    • PHP MySQL Query Function with Easy Error Message
    • MySQL Add Column if Not Exists - PHP Function
    • Who Needs Photoshop? PHP GD Images and Your Online Store
    • Web Developer Toolbar for Firefox
    • Happy New Year from Edmonds Commerce
    • HOSTS file, Vista and Blocking Unwanted Adverts

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

Freelance PHP Web Design UK Commercial Web Design