dongwuge6201
2018-06-20 14:56
浏览 140
已采纳

PHP + SQL - 使用SQL Server的“创建到”脚本通过PHP创建表

In SQL Server you seem to be able to get the CREATE TABLE script by right clicking a table, 'Script Table As...' and selecting 'Create To'. I've copied the output script and while it does seem to work and create the table when pasted onto the 'New Query' window, it does not work when used in PHP. Example code:

$sqlcommand = 
"CREATE TABLE [dbo].[departments](
    [department_pk_fk] [varchar](25) NOT NULL,
    [des] [varchar](100) NULL,
    CONSTRAINT [PK_departamentos] PRIMARY KEY CLUSTERED
(
    [department_pk_fk] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
";
$site_api->execQuery($sqlcommand);

Anyone have any useful input about why this would happen?

Edit: execQuery code:

public function execQuery($q,$scalar=FALSE,$returns=FALSE) {  
//return an assoc array or a scalar
    global $Cfg, $site_api;

    $this->query=$q;


    try {
      $result_array = array();
        $result = $this->conn->query($this->query);
        if (is_bool($result)===TRUE) {  //if reslt is true or false then there is nothing to do
            return $result;
        } else {
            if (!$scalar) {
                if ($result->rowCount()==0)
                    return null;
                //SQLSRV
                while($row = $result->fetchAll(SQLSRV_FETCH_ASSOC)) {
                    $result_array[]=$row;
                }
                $result->closeCursor();
                if($returns) return $result_array[0];
            } else {
                $row = $result->fetchAll(SQLSRV_FETCH_NUMERIC);
                if($returns) return $row[0];
            }
        }
    }
    catch (Exception $e) {
        print "Error Code <br>".$e->getCode();
        print "Error Message <br>".$e->getMessage();
        print "Strack Trace <br>".nl2br($e->getTraceAsString());
    }
}`

图片转代码服务由CSDN问答提供 功能建议

在SQL Server中,您似乎可以通过右键单击表来获取CREATE TABLE脚本,'脚本表为 ......'并选择'创建到'。 我已经复制了输出脚本,虽然它似乎工作并且在粘贴到“新查询”窗口时创建表,但在PHP中使用它时它不起作用。 示例代码: < pre> $ sqlcommand = “CREATE TABLE [dbo]。[departments]( [department_pk_fk] [varchar](25)NOT NULL, [des] [varchar](100)NULL, CONSTRAINT [PK_departamentos] PRIMARY KEY CLUSTERED ( [department_pk_fk] ASC )WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY] ) ON [PRIMARY] GO “; $ site_api-&gt; execQuery($ sqlcommand);

任何人都有任何有用的输入,说明为什么会发生这种情况 编辑: execQuery 代码:

  public function execQuery($ q,$ scalar = FALSE,  $ returns = FALSE){
 //返回一个关联数组或一个标量
全局$ Cfg,$ site_api; 
 
 $ this-&gt; query = $ q; 
 
 
尝试{
  $ result_array = array(); 
 $ result = $ this-&gt; conn-&gt; que  ry($ this-&gt; query); 
 if(is_bool($ result)=== TRUE){//如果reslt为true或false则没有任何事情要做
返回$ result; 
} else {  
 if(!$ scalar){
 if($ result-&gt; rowCount()== 0)
返回null; 
 // SQLSRV 
 while($ row = $ result-&gt; fetchAll(SQLSRV_FETCH_ASSOC)  )){
 $ result_array [] = $ row; 
} 
 $ result-&gt; closeCursor(); 
 if($ returns)返回$ result_array [0]; 
} else {
 $ row  = $ result-&gt; fetchAll(SQLSRV_FETCH_NUMERIC); 
 if($ returns)返回$ row [0]; 
} 
} 
} 
 catch(异常$ e){
 print“错误代码&lt  ; br&gt;“。$ e-&gt; getCode(); 
打印”错误消息&lt; br&gt;“。$ e-&gt; getMessage(); 
打印”Strack Trace&lt; br&gt;“。nl2br($  e-&gt; getTraceAsString()); 
} 
}`
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题