似乎是MySQL中的语法错误

时间:2010-08-19 11:11:29

标签: php mysql

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 个答案:

答案 0 :(得分:6)

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

  

mysql_query()向与指定link_identifier关联的服务器上的当前活动数据库发送唯一查询(不支持多个查询)

答案 1 :(得分:0)

替换这个:

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

答案 2 :(得分:0)

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());