简体   繁体   English

在php中运行多个SQL Server查询

[英]Run multiple SQL Server queries in php

I would like to run several SQL Server queries in php but he does not want to accept that with me. 我想在php中运行几个SQL Server查询,但他不想接受我的要求。 Here is my php script: 这是我的PHP脚本:

<?php
      error_reporting(0);

      $serverName = "SERVERNAME";
      $connectionInfo = array( "UID"=>"Username", "PWD"=>"password", "Database"=>"databasename", "MultipleActiveResultSets"=>true);

      $conn = sqlsrv_connect( $serverName, $connectionInfo);

     if( $conn == false ) {
          die( print_r( sqlsrv_errors(), true));
     }

     $sql = "INSERT INTO TABLE (column) VALUES (?)";
     $tsql = "INSERT INTO TABLE_NEW(column, column, column) Values (?, 'INSERT', 'WEBSERVER')";
     $params = array($_POST["addSomething"]);

       $stmt = sqlsrv_query( $conn, $sql, $params, $tsql, $params);
       if( $stmt == false ) {
         die(print_r( sqlsrv_errors(), true));
       }
       else
       {
           echo '<script language="javascript">';
           echo 'alert(Alert what Happend!)';  //not showing an alert box.
           echo '</script>';
       }
       sqlsrv_close($conn);
       ?>

I took out the private data and replaced it with random names. 我取出了私人数据,并用随机名称代替了它。 I am happy about any help. 我很乐意提供帮助。

You can not pass more than one statement to sqlsrv_query() . 您不能将多个语句传递给sqlsrv_query() In your case you have two options: 在您的情况下,您有两种选择:

  • Generate one complex T-SQL statement. 生成一个复杂的T-SQL语句。 In this case, the number of placeholders must match the number of passed parameters. 在这种情况下,占位符的数量必须与传递的参数的数量匹配。
  • Execute each statement separately 分别执行每个语句

Complex statement: 复杂的声明:

<?php
...
$sql = "
    INSERT INTO TABLE (column) VALUES (?); 
    INSERT INTO TABLE_NEW(column, column, column) Values (?, 'INSERT', 'WEBSERVER')
";
$params = array(
    $_POST["addSomething"],
    $_POST["addSomething"]
);
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt === false ) {
   # Your code here
} else {
   # Your code here
}
...
?>

Notes: 笔记:

PHP Driver for SQL Server supports multiple result sets. 用于SQL Server的PHP驱动程序支持多个结果集。 If you use one complex statement and one or more of your statements are SELECT statements, you need to call sqlsrv_next_result() to make active the next result (first result is active by default). 如果使用一个复杂的语句,而一个或多个语句是SELECT语句,则需要调用sqlsrv_next_result()使下一个结果变为活动状态(默认情况下第一个结果为活动状态)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM