Mysql php dynamically adds the alter table column to the TABLE

Source: Internet
Author: User

Mysql tutorial php tutorial Add a column alter table to the TABLE dynamically
 
The mysql statement for adding columns is
 
Alter table tableName add column columnName varchar (20) not null
 
*/
 
// Database tutorial connection method
$ Db = '111cnnet ';
$ Conn = mysql_pconnect ('localhost', 'root', 'root') or die (mysql_error ());
Mysql_select_db ($ db, $ conn );
 
// Start adding a column Program
 
/*
Row to see the table structure of the inserted list
 
Id int (8) No auto_increment
Title char (150) utf8_general_ci is NULL
Content text utf8_general_ci
 
*/
 
$ Column = 'addnewcolumn ';
$ SQL = "ALTER TABLE test ADD COLUMN $ column varchar (20) not null ";
If (mysql_query ($ SQL ))
{
Echo 'Operation successful ';
}
Else
{
Echo mysql_error ($ conn );
}
 
/*
 
After the operation is successful

Id int (8) No auto_increment
Title char (150) utf8_general_ci is NULL
Content text utf8_general_ci is NULL
AddNewColumn varchar (20) utf8_general_ci No
 
Operation failed if
Duplicate column name 'addnewcolumn'
It proves that addNewColumn already exists.

*/
 
// This article was originally posted on www. bKjia. c0m and indicated the source
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.