我有下面的代碼失敗:CREATE TABLE IF NOT EXISTS當表中存在
$db_host = 'localhost';
$db_port = '3306';
$db_username = 'root';
$db_password = 'root';
$db_primaryDatabase = 'dsl_ams';
// Connect to the database, using the predefined database variables in /assets/repository/mysql.php
$dbConnection = new mysqli($db_host, $db_username, $db_password, $db_primaryDatabase);
// If there are errors (if the no# of errors is > 1), print out the error and cancel loading the page via exit();
if (mysqli_connect_errno()) {
printf("Could not connect to MySQL databse: %s\n", mysqli_connect_error());
exit();
}
$queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS `USERS` (
`ID` int(11) unsigned NOT NULL auto_increment,
`EMAIL` varchar(255) NOT NULL default '',
`PASSWORD` varchar(255) NOT NULL default '',
`PERMISSION_LEVEL` tinyint(1) unsigned NOT NULL default '1',
`APPLICATION_COMPLETED` boolean NOT NULL default '0',
`APPLICATION_IN_PROGRESS` boolean NOT NULL default '0',
PRIMARY KEY (`ID`)
)";
if(!$dbConnection->query($queryCreateUsersTable)){
echo "Table creation failed: (" . $dbConnection->errno . ") " . $dbConnection->error;
}
,輸出...
Table creation failed: (1050) Table '`dsl_ams`.`USERS`' already exists
我不明白的是:是不是IF NOT EXISTS
應該取消SQL查詢的執行,如果該表已經存在?換句話說,如果表存在,它不應該退出if語句,並且根本不回顯任何內容,而不嘗試執行查詢?
只是試圖找到「如果它不存在就創建表」的最佳方式,而不向用戶輸出任何內容。
東西不對。 [見可能愚蠢](http://stackoverflow.com/q/3302476/168868)([和這一個](http://stackoverflow.com/q/7019018/168868))進行一些維護? – Charles
它肯定看起來像是同樣的問題,很好找! –
迄今爲止沒有骰子... http://puu.sh/2TEHp.png - 它真的,真的不存在.. –