期望循环运行一次,但不会

时间:2019-06-18 05:12:50

标签: php sql-server-2017

我已经用PHP编写了完整的逻辑,并且我的数据库处于sql-server正在循环运行的循环中,但是当我使用更多的查询时,我的循环并没有给我想要的结果,因此我无法通过sql-server尝试跟踪在本地浏览器上捕获错误,但无济于事

在第一次查询后,我得到了与众不同的公司和城市名称

co1 ci1, co1 ci2, 二氧化碳, co3 ci4,

现在我要遍历上面的查询结果,以获取不同公司和城市的每个员工

first query (distinct result)
{
while {
        second query(each employee of every distinct company )
         while{
                now applying 3 check through queries  on each employee
              }
       }
}

通过应用检查,我的第二个循环运行了一次以上

$Conn_sis=sqlsrv_connect($ServerName,$ConnectionInfo);
$PSL = array("1", "2", "3");
if($Conn_sis)
{   
    if(($DistinctCompany=sqlsrv_query($Conn_sis,
        "SELECT dbo.WalkerItContacts.ConcatKey , 
         dbo.WalkerItContacts.CompanyName,
         dbo.WalkerItContacts.EmployeeCity,
         dbo.WalkerItContacts.EmployeeAddress1
         FROM dbo.WalkerItContacts
         GROUP BY dbo.WalkerItContacts.ConcatKey,
         dbo.WalkerItContacts.CompanyName,
         dbo.WalkerItContacts.EmployeeCity,
         dbo.WalkerItContacts.EmployeeAddress1
         HAVING count(distinct dbo.WalkerItContacts.CompanyName) = 1
         order by dbo.WalkerItContacts.CompanyName")) !== false)    
         {


        while($DistinctCompany_result = sqlsrv_fetch_object( $DistinctCompany ))
        {

            $DistinctCompanyEmployee = sqlsrv_query($Conn_sis,
                "SELECT *
                 from dbo.WalkerItContacts 
                 where dbo.WalkerItContacts.CompanyName ='$DistinctCompany_result->CompanyName' 
                 AND dbo.WalkerItContacts.EmployeeCity ='$DistinctCompany_result->EmployeeCity'
                 And dbo.WalkerItContacts.EmployeeAddress1 ='$DistinctCompany_result->EmployeeAddress1'");
            while($DistinctCompanyEmployee_result = sqlsrv_fetch_object($DistinctCompanyEmployee ))
            {
                $CCount = sqlsrv_query($Conn_sis,
                "SELECT * 
                 from dbo.WalkerElectedContacts
                 where dbo.WalkerElectedContacts.CompanyName='$DistinctCompanyEmployee_result->CompanyName' 
                 AND dbo.WalkerElectedContacts.EmployeeCity = '$DistinctCompanyEmployee_result->EmployeeCity'",array(), array( "Scrollable" => 'static' ));
                 $CCount_result = sqlsrv_num_rows($CCount);
                ;
                 if ($CCount_result >= 2)
                 {
                         echo "Error in retrieveing row count.";
                 }
                 else
                 {
                    $EmployeeTital = sqlsrv_query($Conn_sis,
                        "SELECT dbo.WalkerElectedContacts.EmployeeTitle            
                         from dbo.WalkerElectedContacts
                         where dbo.WalkerElectedContacts.CompanyName='$DistinctCompanyEmployee_result->CompanyName' 
                         AND dbo.WalkerElectedContacts.EmployeeCity = '$DistinctCompanyEmployee_result->EmployeeCity'");
                    while($EmployeeTital_result = sqlsrv_fetch_object($EmployeeTital))
                    {

                        if($EmployeeTital_result->EmployeeTitle === $DistinctCompanyEmployee_result->EmployeeTitle)
                        {
                            echo "same";
                        }
                        else
                        {

                            if(in_array($DistinctCompanyEmployee_result->PrioritySL, $PSL))
                            {
                                $InserQuery = sqlsrv_query($Conn_sis,"INSERT INTO dbo.WalkerElectedContacts(CompanyName, EmployeeCity,EmployeeTitle,EmployeeSeniorityLevel,PrioritySL) 
                                Values('$DistinctCompanyEmployee_result->CompanyName','$DistinctCompanyEmployee_result->EmployeeCity','$DistinctCompanyEmployee_result->EmployeeTitle','$DistinctCompanyEmployee_result->EmployeeSeniorityLevel','$DistinctCompanyEmployee_result->PrioritySL')");
                            }
                            else
                            {
                                echo"not";
                            }
                    }
                 }




            }

        }
     }
}

else
{
    die("query is problem");
}

我希望在第二个循环中检查我的每个员工并插入新表中,然后跳转到下一个不同公司的第一个循环

0 个答案:

没有答案