通过PHP运行CREATE TABLE查询
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.
更多相关文章
- 如何更改select语句中的列值
- MySql 表管理常用的sql语句
- 可以在SELECT语句中嵌入描述语法吗?
- 如何从MySQL DBs的不同表中提取create语句?
- mysql查询一条工单时间需要10秒。优化sql语句得以解决。
- MySql SELECT 语句执行顺序
- MySQL 绿色版基本设置语句
- MySQL表格查询基本语句2
- MySQL DELETE语句和TRUNCATE TABLE语句的区别