php CREATE TABLE IF NOT EXISTS 在表存在时失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16554735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:21:28  来源:igfitidea点击:

CREATE TABLE IF NOT EXISTS failing when the table exists

phpmysqlmysqlimysql-error-1050

提问by Samuel Stiles

I have the following code:

我有以下代码:

$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;
}

Which outputs...

哪个输出...

Table creation failed: (1050) Table '`dsl_ams`.`USERS`' already exists

What I don't understand is: isn't IF NOT EXISTSsupposed to cancel the execution of the SQL query if that table already exists? In other words, if the table exists, shouldn't it exit that if statement and not echo anything out at all, and not attempt to execute the query?

我不明白的是:IF NOT EXISTS如果该表已经存在,不应该取消 SQL 查询的执行吗?换句话说,如果该表存在,它是否应该退出该 if 语句并且根本不回显任何内容,并且不尝试执行查询?

Just trying to find the best way to "create a table if it doesn't exist" without outputting anything to the user.

只是试图找到“如果表不存在则创建表”的最佳方法,而不向用户输出任何内容。

采纳答案by ERed

You should get a warning, not an error. What version are you running? Anyway if you want to display erros type this before your SQL query: SET sql_notes = 0; and then type SET sql notes=1; after the query.

您应该收到警告,而不是错误。你运行的是什么版本?无论如何,如果你想在你的 SQL 查询之前显示错误类型:SET sql_notes = 0; 然后输入 SET sql notes=1; 查询后。

回答by eaglewu

I use your script in mysql is true:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.30    |
+-----------+
1 row in set (0.01 sec)

$queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS `sun_channel` (
    `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`)
)";
$res = mysql_query($queryCreateUsersTable);
var_dump($res);
die;

//show bool(true)