我的代码出了什么问题?
$query = "CREATE TABLE IF NOT EXISTS 'data_table' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'name' VARCHAR(255) NOT NULL,
'active' TINYINT NOT NULL DEFAULT '1',
'create_date' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'income' INT DEFAULT NULL,
PRIMARY KEY ('id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
$createTable = mysqli_query($connect, $query);
答案 0 :(得分:1)
表格和列名称不需要单引号而不用:您可以使用" or backticks
代替。
CREATE TABLE IF NOT EXISTS data_table(
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
name VARCHAR( 255 ) NOT NULL ,
active TINYINT NOT NULL DEFAULT '1',
create_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
income INT DEFAULT NULL ,
PRIMARY KEY ( id )
) ENGINE = MYISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT =1
答案 1 :(得分:0)
尝试:
$query = "CREATE TABLE IF NOT EXISTS `data_table` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`active` TINYINT NOT NULL DEFAULT 1,
`create_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`income` INT DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
请注意,单引号将替换为反引号