代码之家  ›  专栏  ›  技术社区  ›  Rob

似乎是我的MySQL中的语法错误

  •  1
  • Rob  · 技术社区  · 16 年前

    MySQL推迟了以下错误:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `badips` ( `id` int(10) NOT NULL auto_increment, ' at line 2
    

    当我运行以下PHP时:

    if (file_exists("../login/includes/config.php")) {
    
        $db_schema = array();
    
    $db_schema[] = "DROP TABLE IF EXISTS `badips`;
    CREATE TABLE IF NOT EXISTS `badips` (
      `id` int(10) NOT NULL auto_increment,
      `host` varchar(50) NOT NULL,
      `ip` varchar(20) NOT NULL,
      `enteredhost` varchar(50) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
    
    require_once('../login/includes/config.php');
    require_once('open-db.php');
    
          echo "<h3>Creating tables...</h3>";
          foreach($db_schema as $sql) {
           mysql_query($sql) or die(mysql_error());
          }
          echo "<h3>Done!</h3>";
      }
    

    但是当我从phpmyadmin运行相同的SQL时,它的工作没有任何缺陷。我不知道是什么问题。有人知道吗?

    3 回复  |  直到 16 年前
        1
  •  6
  •   Daniel Vassallo    16 年前

    mysql_query() 不支持多个查询。 Quoting the PHP Manual :

    MySqLQueQuess() 发送唯一查询 (不支持多个查询) 到与指定链接标识符关联的服务器上当前活动的数据库。

        2
  •  0
  •   Gatman    16 年前

    替换此:

    $db_schema[] = "DROP TABLE IF EXISTS `badips`;
    CREATE TABLE IF NOT EXISTS `badips` (
      `id` int(10) NOT NULL auto_increment,
      `host` varchar(50) NOT NULL,
      `ip` varchar(20) NOT NULL,
      `enteredhost` varchar(50) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
    

    具有

    $query = "DROP TABLE IF EXISTS `badips`;
    CREATE TABLE IF NOT EXISTS `badips` (
      `id` int(10) NOT NULL auto_increment,
      `host` varchar(50) NOT NULL,
      `ip` varchar(20) NOT NULL,
      `enteredhost` varchar(50) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26";
    
    $db_schema = explode(";",$query);
    
        3
  •  0
  •   Daniel Vassallo    16 年前

    mysql_query()不支持多个查询,替换为

    $sql="DROP TABLE IF EXISTS `badips`;";
    
    mysql_query($sql) or die(mysql_error());
    
    $sql="CREATE TABLE IF NOT EXISTS `badips` (`id` int(10) NOT NULL auto_increment, `host` varchar(50) NOT NULL, `ip` varchar(20) NOT NULL, `enteredhost` varchar(50) NOT NULL, PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
    
    mysql_query($sql) or die(mysql_error());
    
    推荐文章