如何在sqlsrv查询结果中删除空格和属性

时间:2019-07-19 08:34:54

标签: php arrays sql-server-2012

我要删除sqlsrv查询结果中的空格和属性。

这是我的sqlsrv函数,返回该函数将返回数组

function getDatasvr(){

    $connectionInfo = array( "Database"=>"$dbname", "UID"=>"$uid", "PWD"=>"$pwd");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

        $tsql_critical = "SELECT ttdsls401888.t_item as partno, ttcibd001888.t_dsca as partname, sum(ttdsls401888.t_qidl) as total
                        FROM ttdsls401888,ttcibd001888 
                        WHERE ttdsls401888.t_item = ttcibd001888.t_item
                        AND ttdsls401888.t_cwar='PHNBBF' 
                        AND ttdsls401888.t_dldt >= '$today' 
                        AND ttdsls401888.t_stbp = 'P00000002'
                        group by ttdsls401888.t_item,ttcibd001888.t_dsca
                    ";


    /* --Execute the query-- */    
    $stmt_critical = sqlsrv_query( $conn, $tsql_critical); 

    $result = array(); 


    while ($row = sqlsrv_fetch_array($stmt_critical,SQLSRV_FETCH_ASSOC))
    {
    array_push($result, $row);

    } 

    sqlsrv_free_stmt($stmt_critical); //free the connection
    sqlsrv_close($conn); //Close the connnection first

    return $result;


}

上面的查询返回的结果类似于此数组,其中数组中有空格和属性。

array(54) {
  [0]=>
  array(3) {
    ["partno"]=>
    string(21) "         PW932479X1-2"
    ["partname"]=>
    string(21) "REAR END ASSY (ML/HL)"
    ["total"]=>
    float(15)
  }
  [1]=>
  array(3) {
    ["partno"]=>
    string(17) "         PW935615"
    ["partname"]=>
    string(30) "EXT ASSY BRACE WHSE INR FRT LH"
    ["total"]=>
    float(120)
  }
  [2]=>
  array(3) {
    ["partno"]=>
    string(17) "         PW935412"
    ["partname"]=>
    string(21) "FUEL FILLER DOOR ASSY"
    ["total"]=>
    float(60)
  }
}

但是我希望结果没有像这样的空格和属性

array(54) {
  [0]=>
  array(3) {
    ["partno"]=>"PW932479X1-2"
    ["partname"]=>"REAR END ASSY (ML/HL)"
    ["total"]=> 15
  }
  [1]=>
  array(3) {
    ["partno"]=>"PW935615"
    ["partname"]=>"EXT ASSY BRACE WHSE INR FRT LH"
    ["total"]=>120
  }
  [2]=>
  array(3) {
    ["partno"]=>"PW935412"
    ["partname"]=>"FUEL FILLER DOOR ASSY"
    ["total"]=>60
  }
}

很抱歉出现任何术语错误,刚刚学会了在2天内查询sqlsrv。 任何帮助,将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用TRIM()

$tsql_critical = "SELECT TRIM(ttdsls401888.t_item) as partno, TRIM(ttcibd001888.t_dsca) as partname, TRIM(sum(ttdsls401888.t_qidl)) as total
                        FROM ttdsls401888,ttcibd001888 
                        WHERE ttdsls401888.t_item = ttcibd001888.t_item
                        AND ttdsls401888.t_cwar='PHNBBF' 
                        AND ttdsls401888.t_dldt >= '$today' 
                        AND ttdsls401888.t_stbp = 'P00000002'
                        group by ttdsls401888.t_item,ttcibd001888.t_dsca
                    ";