tags:

views:

57

answers:

3

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 same SQL from PHPMyAdmin, it works without any flaws. I can't figure out what the problem is. Anyone know?

+6  A: 

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.

Daniel Vassallo
Well, I learned something new today. Split it into multiple mysql_query()'s and it worked. Thanks, Daniel.
Rob
A: 

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);
Gatman
A: 

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