www.webdeveloper.com
Results 1 to 2 of 2

Thread: PHP sqlsrv store procedure sometimes no return result

  1. #1
    Join Date
    Apr 2014
    Posts
    1

    PHP sqlsrv store procedure sometimes no return result

    I have this strange problem with PHP Sqlsrv calling a store procedure. Sometimes it return result, sometimes not. When debugging with netbean php, it popup Socket exception occurred. When the browser made the request, the connection get reset and does not return any error.

    I am using the sqlsrv library of codeigniter

    PHP Code:
      function result_object()
            {
                if (
    count($this->result_object) > 0)
                {
                    return 
    $this->result_object;
                }
        
                
    // In the event that query caching is on the result_id variable
                // will return FALSE since there isn't a valid SQL resource so
                // we'll simply return an empty array.
                
    if ($this->result_id === FALSE OR $this->num_rows() == 0)
                {
                    return array();
                }
        
                
    $this->_data_seek(0);
                while (
    $row $this->_fetch_object())
                {
                    
    $this->result_object[] = $row;
                }
        
                return 
    $this->result_object;
            }
        
        function 
    result($type 'object')
            {
                if (
    $type == 'array') return $this->result_array();
                else if (
    $type == 'object') return $this->result_object();
                else return 
    $this->custom_result_object($type);
            }
        
        
    $result null;
                do{
                    
    $result $this->result(); //error happen on this line.
                      
    log_message("error"print_r("abc123",true));  // if added this line, the result is okay most of the time
                    
    }while( sqlsrv_next_result($this->result_id)); 
    **EDIT :**
    If I add this line after $result then it works MOST of the TIME . log_message("error", print_r("abc123",true));

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    It's hard to tell where the error may be since you're calling functions that you haven't shown code for but more importantly, you haven't shown the code for where you're calling the stored procedure. It should be written the same as any query, however, I haven't used CodeIgniter's library for sqlsrv before so I'm not sure if they write theirs differently:

    PHP Code:
    $sql "{ call myProcedure(?, ?) }";
    $stmt sqlsrv_query($conn$sql, array(1"abc"));
    if(!
    $stmt) {
         die(
    print_r(sqlsrv_errors(), true));
    }
    // you can use sqlsrv_fetch_array or sqlsrv_next_result as 
    // well as sqlsrv_fetch_object
    while($resObj sqlsrv_fetch_object($stmt)) {
        
    // do something with it 

    It may not be an error in your code, otherwise you should see the same result everytime, so I'm guessing caching is on in your PHP settings. In your code, you're only returning an empty array if it is on. Try setting caching to false.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles