SQL Server查询未按预期执行PHP

时间:2017-12-01 20:39:24

标签: php jquery ajax sql-server-2012 sqlsrv

我在使用ajax实现jquery自动完成时遇到困难,同样的功能适用于另一个表,但是在2个表(sales_man,Cashcustomer)中它的空数组,但是第一个表(customerMaster)它工作正常并列出每个cust作为其请求。 我的代码

myApp.js

$('#custName').autocomplete({ 
      minLength: 1,
      autoFocus:true,
      source: function( request, response ) {

          $.ajax({
              url: "/test/ajax/ajaxOpRespond.php",
              data: {action:'Get_CashCust_Code', code:request.term},
              dataType:'JSON',
              type: "POST",
              success: function( data ) {
                var result = [];

                if(data.length > 0){
                  for(var i=0;i<data.length;i++){  

                  result[i] = { "value" :data[i].CUST.CUST_NAME+" ("+data[i].CUST.TELE_NO+")", 
                 "custName":data[i].CUST.CUST_NAME,
                 "custtel1":data[i].CUST.TELE_NO,


                };
                  }
                }      
                else{
                  alert("jesha mesha");
                  $('#custType').val("new");
                }                                     
                  response(result);
              },
              error: function(xhr, textStatus, errorThrown){                    
               alert("fail"+errorThrown);
              var result = [];
              response( result );
          }
          });
      },
      select: function( event, ui ) {
        $("#custName").val(ui.item.custName);

        $('#custTel1').val(ui.item.custtel1);$('#custTel1').attr("readonly","true");
        $('#custType').val("old");



          return false;
       }
    });

ajaxController.php

case 'Get_CashCust_Code':
            $code = $_POST['code'];

            $msql = "SELECT [ID]
  ,[CUST_NAME]
  ,[TELE_NO] FROM [dbo].[tbl_CashCustomer_Master] WHERE [CUST_NAME] LIKE '%{$code}%' ;";
            $stmt = sqlsrv_query( $conn, $msql);
            if( $stmt === false ) {
                die( print_r( sqlsrv_errors(), true));
                    }
            if( sqlsrv_fetch( $stmt ) === false || sqlsrv_has_rows( $stmt ) === false) {
                    die( print_r( sqlsrv_errors(), true));
                    }
                    $rows = array();

            while( $r = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
                $rows[] = array('CUST' => $r);

            }
            sqlsrv_free_stmt( $stmt);

            echo json_encode($rows);


            break;

output on Server studio

在ajax请求中它只返回&#39; []&#39;

请帮助

1 个答案:

答案 0 :(得分:0)

经过几个小时的搜索和尝试后,我发现了我的问题,当我发布我的问题时,我不知道这里真正的问题是什么以及为什么它返回空,我的实际问题是第一行被排除在响应数组之外我添加了相同的客户2次只有一个在自动完成列出,这是因为我执行并同时获取查询2次

     case 'Get_CashCust_Code':
                    $code = $_POST['code'];

                    $msql = "SELECT [ID]
          ,[CUST_NAME]
          ,[TELE_NO] FROM [dbo].[tbl_CashCustomer_Master] WHERE [CUST_NAME] LIKE '%{$code}%' ;";
                    $stmt = sqlsrv_query( $conn, $msql);
                    if( $stmt === false ) {
                        die( print_r( sqlsrv_errors(), true));
                            }
    /*sqlsrv_fetch( $stmt ) is fetching all rows */         
if( sqlsrv_fetch( $stmt ) === false || sqlsrv_has_rows( $stmt ) === false)  {
                            die( print_r( sqlsrv_errors(), true));
                            }
                            $rows = array();

     /*sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) fetching rows for the second time*/              
 while( $r = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
                        $rows[] = array('CUST' => $r);

                    }
                    sqlsrv_free_stmt( $stmt);

                    echo json_encode($rows);


                    break;

通过阅读解决方案 here他提到了我在执行指针时所理解的执行机制从1开始而不是0

在下面引用他对这个问题的回答

  

sqlsrv_fetch_array()方法检索记录并前进   光标一个。你正在循环你的第190行的记录   虽然循环,但在第21行你有这个:

     

$ auditData = sqlsrv_fetch_array($ stmt,SQLSRV_FETCH_ASSOC),

     

所以你已经获得了第一张唱片。等到你的时候   在第190行循环,你已经在记录2。

     

你有几个选择。一种是将你的while循环改为a   do / while循环:

     

while {...} while($ row = sqlsrv_fetch_array($ stmt,   SQLSRV_FETCH_ASSOC))

     

您需要将第21行(及相关行)更改为$ row =   ...而不是$ auditData = ...

     

或者,您可以在开始使用之前重置指针   环。不记得如何使用SqlServer驱动程序,但一个   快速谷歌应该让你走在正确的轨道上。

我实际上已经厌倦了做同样的问题,那时候我改变了

  if( sqlsrv_fetch( $stmt ) === false || sqlsrv_has_rows( $stmt ) === false) {
                    die( print_r( sqlsrv_errors(), true));
                    }

/* removed the first fetch */
     if(sqlsrv_has_rows( $stmt ) === false) {
                        die( print_r( sqlsrv_errors(), true));
                        }

现在它正常工作正常,感谢您的帮助和@Psi感谢您通知我SQL注入攻击