为什么我的While块无法运行

时间:2016-07-20 05:47:06

标签: php sql-server

我尝试使用此代码从数据库中获取记录,但我的阻止云对我没有响应。我回应我的查询它将打印完美,但复制查询运行我的服务器时,它不会返回值。这个鳕鱼产生错误“Undefined variable:json”,因为它忽略了While block。感谢Adv

<p>

1 个答案:

答案 0 :(得分:0)

您尝试在第二维数组中追加数据,甚至不首先初始化数组。

首先需要初始化$json数组变量,然后需要在$json['purchase_pending_repo']初始化第二个数组。

<?php
    require "db_config.php";

    $cus_name="R M PLASTICES";

    if(isset($cus_name) && $cus_name!=NULL && $cus_name!="")
    {
        $sql="select Vo.Cus_Name,Vo.vendorid,Vo.Created_date,vm.raw_mat as MATERIAL,vm.uom as UOM,vm.qty as ORDERQTY,vm.qtyin as RECDQTY,convert(decimal(7,3),sum(vm.qty-vm.qtyin)) as BALQTY from mmsuser.vendor_mat vm inner join mmsuser.vendor_order vo on vm.vendorid=vo.vendorid where Vo.Cus_Name='".$cus_name."'group by vm.Raw_mat,vm.qty,vm.uom,vm.qtyin,Vo.Cus_Name,Vo.vendorid,vm.id,Vo.Created_date having sum(vm.qty-vm.qtyin) > 0 order by vm.raw_mat";
    }

    $stmt=sqlsrv_query($conn, $sql);
    if($stmt===false) 
    {
        die(print_r(sqlsrv_errors(),true));
    }
    $json = array('purchase_pending_repo' => array()); //OR $json = ['purchase_pending_repo'=>[]]
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
    {
      $json['purchase_pending_repo'][]=$row;
    }
    sqlsrv_free_stmt($stmt);

    echo json_encode($json);

    sqlsrv_close($conn);  

?>