Search This Blog

Friday, 9 March 2012

Add column to mysql table if it does not exist in the table

<?php
$DBHOST = 'HOSTNAME';
$DBUSER = 'USERNAME';
$DBPASS = 'PASSWORD';
$DATABASE = 'DBNAME';
$table = 'talble_name';
$column = "column_name";

mysql_connect($DBHOST,$DBUSER,$DBPASS) or die(mysql_error());
mysql_select_db($DATABASE) or die(mysql_error());
$result=mysql_query("SELECT $column FROM $table;");
if (mysql_errno())
{
        echo "Unknown column CREATE one first";

        // Alter table add column goes here
}
else
{
          echo "column exists";
}
mysql_close();
?>

 

No comments:

Post a Comment