如果我向查询字符串添加条件,sqlsrv_query返回false

时间:2019-05-29 11:26:21

标签: php sql

我正在从 sqlsrv_query()获取数据,并将其推入数组,然后将其添加到 pg_query(with ARRAY())。当我第一次无条件运行查询时,仅执行SELECT,它仅返回273行(应为21225)。然后,我向ID添加一个条件,使其大于273,但是sqlsrv_query返回false。拜托,帮忙...我约2周无法解决

<?php
   //INCREASE MEMORY LIMIT TO STORE BIG AMOUNT OF DATA
   ini_set('memory_limit', '1024*1024');
   /////////////GET OBJECTS FROM MSSQL
   $conInfo = array('UID' => 'test', 'PWD' => 'test', 'Database' => 'KTMOTIS' );
   $serverName = "real address";//There is real IP-address instead
   $connect = sqlsrv_connect($serverName, $conInfo);
   $base_ids = array();
   $otis_numbers = array();
   $names = array();
   $addresses = array();
   $phones = array();
   $unit_numbers = array();
   $unittype_ids = array();
   $is_protected = array();
   $org_ids = array();
   $old_ids = array();
   $count = 0;

   if ($connect) {
      $tsql = "select Convert(INT, BASENUMBER) AS BASENUMBER, Convert(INT, OTISNUMBER) AS OTISNUMBER, ";
      $tsql .= "Convert(VARCHAR(255), OBJECTNAME) AS OBJECTNAME, Convert(VARCHAR(255), INFO) AS INFO, ";
      $tsql .= "Convert(VARCHAR(255), PHONES) AS PHONES, Convert(INT, UNITNUMBER) AS UNITNUMBER, ";
      $tsql .= "Convert(INT, UNITTYPE) AS UNITTYPE, Convert(INT, ISPROTECTED) AS ISPROTECTED, ";
      $tsql .= "Convert(INT, ORGID) AS ORGID, Convert(INT, CARDID) AS CARDID from CARDS; ";
      //$tsql.= "WHERE Convert(INT, CARDID) > 1568 ;"
      $stmt = sqlsrv_query($connect, $tsql);
      echo "<br>";
      if ($stmt === false) {
         echo "<br>";
         echo "Error in executing query.</br>".sqlsrv_errors();
         die(print_r(sqlsrv_errors(), true));
      }
      else{
         while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC )) {
            array_push($base_ids, $row["BASENUMBER"]);
            array_push($otis_numbers, $row["OTISNUMBER"]);
            $obj_name = $row["OBJECTNAME"];
            $obj_name = str_replace('"', '', $obj_name);
            $obj_name = str_replace(',', ' и ', $obj_name);
            $count = $count + 1;
            //echo 'OBJECT: '.$obj_name;
            array_push($names, $obj_name);
            $obj_info = $row["INFO"];
            $obj_info = str_replace('"', '', $obj_info);
            $obj_info = str_replace(',', ' и ', $obj_info);
            array_push($addresses, $obj_info);
            $obj_phone = $row["PHONES"];
            $obj_phone = str_replace('"', '', $obj_phone);
            $obj_phone = str_replace(',', ' и ', $obj_phone);
            array_push($phones, $obj_phone);
            array_push($unit_numbers, $row["UNITNUMBER"]);
            array_push($unittype_ids, $row["UNITTYPE"]);
            array_push($is_protected, $row["ISPROTECTED"]);
            array_push($org_ids, $row["ORGID"]);
            array_push($old_ids, $row["CARDID"]);
       }
       echo "<br>";
       echo "COUNT: ".$count;
       /*Convert AS JSON*/
       $base_ids = json_encode($base_ids, JSON_UNESCAPED_UNICODE);
       $otis_numbers = json_encode($otis_numbers, JSON_UNESCAPED_UNICODE);
       $names = json_encode($names, JSON_UNESCAPED_UNICODE);
       $addresses = json_encode($addresses, JSON_UNESCAPED_UNICODE);
       $phones = json_encode($phones, JSON_UNESCAPED_UNICODE);
       $unit_numbers = json_encode($unit_numbers, JSON_UNESCAPED_UNICODE);
       $unittype_ids = json_encode($unittype_ids, JSON_UNESCAPED_UNICODE);
       $is_protected = json_encode($is_protected, JSON_UNESCAPED_UNICODE);
       $org_ids = json_encode($org_ids, JSON_UNESCAPED_UNICODE);
       $old_ids = json_encode($old_ids, JSON_UNESCAPED_UNICODE);

       /*INSERT OR UPDATE OBJECTS IN POSTGRESQL*/
       $con = pg_connect("host=real IP port=5432 dbname=technics user=postgres password=1234") or die('Could not connect: ' . pg_last_error());
       $query = "SELECT * FROM techpart.update_objects(ARRAY " . $base_ids . ", ARRAY " . $otis_numbers . ", ARRAY " . $names . ", ARRAY " . $addresses . ", ARRAY " . $phones . ", ARRAY " . $unit_numbers . ", ARRAY " . $unittype_ids . ", ARRAY " . $is_protected . ", ARRAY " . $org_ids . ", ARRAY " . $old_ids . ");";
       $query = str_replace('"', "'", $query);
       $query = str_replace("L'A", "LA", $query);
       //echo $query;
       echo "<br>";
       if (sizeof($old_ids) > 0) {
           echo "ROW: ". var_dump($row);
           $rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
           $row = pg_fetch_row($rs);
           if ($row[0] == true) {
               echo "Right!";
           }
           else{
               echo "Wrong!";
           }
       }
       else{
           echo "Empty!";
       }
       pg_close();

     }

 }
 else {
     echo "Connection not established.\n";
     die( print_r(sqlsrv_errors(), true));
 }
 sqlsrv_free_stmt($stmt);
 sqlsrv_close($connect);

?>

我已经搜索了有关PHP数组(没有限制)和字符串的限制。 然后我添加以下行:

  

ini_set('memory_limit','1024 * 1024');

使用参数执行sqlsrv_query()。仍然不起作用。它甚至没有遍历$ row = sqlsrv_fetch_array($ stmt,SQLSRV_FETCH_ASSOC){}

返回的结果

  

布尔(假)行:   警告:pg_query():查询失败:错误:无法确定空数组的类型第1行:SELECT * FROM techpart.update_objects(ARRAY [],ARRAY [],AR ... ^提示:显式强制转换为所需类型第84行的/var/www/position/test/upload_main_mssql_data.php中的示例ARRAY [] :: integer []。   无法执行查询:SELECT * FROM techpart.update_objects(ARRAY [],ARRAY [],ARRAY [],ARRAY [],ARRAY [],ARRAY [],ARRAY [],ARRAY [],ARRAY [],ARRAY [] );

0 个答案:

没有答案