简体   繁体   中英

how to add new columns in mysql php

whats my problem to add new columns to DB

//  Connection
global $tutorial_db;

$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");

//  Check Connection
if ($tutorial_db->connect_errno) {
    printf("Connect failed: %s\n", $tutorial_db->connect_error);
    exit();
}
    $query="alter table groups add order varchar (20)";
    $result = $tutorial_db->query($query);

This code doesn't do anything...

ORDER是保留字,您需要用反引号引起来,或者为该列使用其他名称。

$query="alter table groups add `order` varchar (20)";

I suspect it's because your column is called 'order', which is an SQL keyword. If you really want to call it order, enclose it with backticks -

$query="alter table groups add `order` varchar (20)";

But I'd recommend you call it something else if you can.

ALTER TABLE `table_name` add COLUMN `column_name` VARCHAR(20);

Please note the backticks. They are important to escape the column name, especially if you wish to call your column order as that is already a reserve word in mysql and has it's own meaning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM