开发者

Seems to be a syntax error in my MySQL

MySQL is putting off the following error:

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

When I run the following 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>";
  }

But when I run the开发者_如何学C same SQL from PHPMyAdmin, it works without any flaws. I can't figure out what the problem is. Anyone know?


mysql_query() does not support multiple queries. Quoting the PHP Manual:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.


replace this:

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

with

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


mysql_query() does not support multiple queries., replace with

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜