I'm running the following query throug a PHP script

我正在通过PHP脚本运行以下查询

DROP TABLE IF EXISTS `address`;CREATE TABLE `address` (  `Id`
int(11) NOT NULL auto_increment,   `EntityId` int(11) NOT NULL,  
`sStreet1` varchar(50),   `sStreet2` varchar(50),   `sCity`
varchar(50),   `sLoc` varchar(50),   `sPv` char(2),   `sState`
varchar(50),   `sZip` varchar(10),   `sType` varchar(50),  
`FiscalAddress` tinyint(1) NOT NULL,   PRIMARY KEY  (`Id`) );

I can run this query trough different clients (such as Navicat) without problems, but running this through PHP will output the following

我可以通过不同的客户端(例如Navicat)运行此查询而不会出现问题,但通过PHP运行此命令将输出以下内容

$result = mysql_query($query);
if (!$result) {
    $message  = 'Invalid import content: ' . mysql_error() . "\n\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

this will output the following:

这将输出以下内容:

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 address ( Id int(11) NOT NULL AUTO_INCREMENT, EntityId int(11) ' at line 2 Whole query: DROP TABLE IF EXISTS address; CREATE TABLE address ( Id int(11) NOT NULL AUTO_INCREMENT, EntityId int(11) NOT NULL, sStreet1 varchar(50), sStreet2 varchar(50), sCity varchar(50), sLoc varchar(50), sPv char(2), sState varchar(50), sZip varchar(10), sType varchar(50), FiscalAddress tinyint(1) NOT NULL, PRIMARY KEY (Id) );

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第2行的'CREATE TABLE地址(Id int(11)NOT NULL AUTO_INCREMENT,EntityId int(11)'附近使用正确的语法。整个查询:DROP TABLE IF EXISTS地址;创建TABLE地址(Id int(11)NOT NULL AUTO_INCREMENT,EntityId int(11)NOT NULL,sStreet1 varchar(50),sStreet2 varchar(50),sCity varchar(50),sLoc varchar(50),sPv char(2), sState varchar(50),sZip varchar(10),sType varchar(50),FiscalAddress tinyint(1)NOT NULL,PRIMARY KEY(Id));

what am I doing wrong???

我究竟做错了什么???

2 个解决方案

#1


2

The issue is not the CREATE TABLE statement, but rather that mysql_query() does not support executing multiple statements in one call. Execute the DROP TABLE statement first in a separate mysql_query() call, then execute the CREATE TABLE.

问题不是CREATE TABLE语句,而是mysql_query()不支持在一次调用中执行多个语句。首先在单独的mysql_query()调用中执行DROP TABLE语句,然后执行CREATE TABLE。

$dt = mysql_query("DROP TABLE IF EXISTS `address`");
if ($dt) {
  // Execute CREATE TABLE separately...
  $ct = mysql_query("CREATE TABLE...........");
  if (!$ct) {
     // Proceed with the rest of your code.
    $message  = 'Invalid import content: ' . mysql_error() . "\n\n";
    $message .= 'Whole query: ' . $query;
    die($message);
  }
}
else {} // DROP TABLE call failed. Handle appropriately.

更多相关文章

  1. 如何更改select语句中的列值
  2. MySql 表管理常用的sql语句
  3. 可以在SELECT语句中嵌入描述语法吗?
  4. 如何从MySQL DBs的不同表中提取create语句?
  5. mysql查询一条工单时间需要10秒。优化sql语句得以解决。
  6. MySql SELECT 语句执行顺序
  7. MySQL 绿色版基本设置语句
  8. MySQL表格查询基本语句2
  9. MySQL DELETE语句和TRUNCATE TABLE语句的区别

随机推荐

  1. Android SDK Manager更新报错——Downloa
  2. android开发之权限问题整理
  3. Android高手进阶教程(九)之----Android H
  4. Android 虚拟摇杆,多种模式回调,返回距离级
  5. Android TV蓝牙模块
  6. Android 服务端开发之开发环境配置
  7. Android 实现气泡布局/弹窗,可控制气泡尖
  8. SurfaceFlinger学习之路(一)View的绘制流程
  9. Android快速滚动
  10. androidmanifest.xml高级属性解析