Topic: How to use ""ALTER TABLE" and add a column?

Hi,

I need to add a column using alter table query. here is the query string i used. but it dooesnot work for me.

$kv_sql = "ALTER TABLE ".TB_PREF."table_name ADD ".db_escape($cust_id)." INT(10) NOT NULL" ; 

    db_query($kv_sql, "The employee attendance could not be added");

But t doesnot work in it. 
i dont know the bug.

Subscription service based on FA
HRM CRM POS batch Themes

Re: How to use ""ALTER TABLE" and add a column?

A couple of things

Is the table name really hard coded to be TB_PREFtable_name?  Or is table_name a variable in which case it should be $table_name.  It would make more sense to me for the column name you are adding to be a constant e.g. 'some_column' rather than a variable.

Does the database user have permission to alter table structure?  Most likely does worth checking though.

Check the syntax against this:

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://dev.mysql.com/doc/refman/5.1/en/create-table.html

You can also get good help on sql here:

http://stackoverflow.com/

ALTER TABLE  `0_users` ADD  `test` INT( 10 ) NOT NULL ;

Cambell https://github.com/cambell-prince

Re: How to use ""ALTER TABLE" and add a column?

you can print the sql query with the confir set to true and just run it in the phpmyadmin to see which part of the query has something wrong

Re: How to use ""ALTER TABLE" and add a column?

Thanks both of you.
Barbarian, i tried the code with PHPmyadmin. it works fine there.

Cambell,  Actually the user is default user in phpmyadmin.

the query is not working with the TR_PREF and $Cust_id. while using the above code.  And my query failed to work.

And i tried to show the sql queries by enabling the option on config.php.  but it showed " ROLLBACK"

What is the issue in it.

Subscription service based on FA
HRM CRM POS batch Themes

Re: How to use ""ALTER TABLE" and add a column?

the query worked, and it created a column. but it immediately executed the " ROLLBACK" query, than it will be reveresed.

Subscription service based on FA
HRM CRM POS batch Themes

6 (edited by apmuthu 09/18/2014 03:34:40 am)

Re: How to use ""ALTER TABLE" and add a column?

$cust_id should be a string with the name of the field and not it's value. If it is a reserved keyword then backquote it.