SQLSRV and multiple selects in Stored Procedure

前端 未结 2 1828
予麋鹿
予麋鹿 2021-01-13 13:39

I have a Stored Procedured which creates a temporary table (#test), fills it with data from another table, runs 3 selects on this temporal table and drops it.

The or

相关标签:
2条回答
  • 2021-01-13 14:21
    // this example for storing the data in drop down menu  - php
    
       // need to connect data base first
        $connectionInfo = array( "Database"=>$database, "UID"=>DATABASE_USER, "PWD"=>DATABASE_PASSWORD);
        $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
        ///  $SQLquery  =  NOTE write sql query here
    
        $stmt = sqlsrv_query( $conn, $SQLquery );
            if( $stmt === false) {
                die( print_r( sqlsrv_errors(), true) );
            } else {
                Display("SQLquery  executed");
            }
            $result = array();  
              $fetchLimit = 0;  // control the infinite loop 
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    
                    //echo $row['COLUMN_NAME']. "<br>";
                    // Loop through each result set and add to result array
                    $result[] = $row['COLUMN_NAME'];          
    
               // results store here  = you need to change  'COLUMN_NAME' based on yoru sql query received data name
    
                    $fetchLimit++;
                    if($fetchLimit>60000)
                    break;
            }
            echo dropdown( "test", $result, 1000 );
    
    
    
        add this function for storing in drop down 
    
        function dropdown( $name, array $options, $selected=null )
        {
            /*** begin the select ***/
            $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";
    
            $selected = $selected;
            /*** loop over the options ***/
            foreach( $options as $key=>$option )
            {
                /*** assign a selected value ***/
                $select = $selected==$key ? ' selected' : null;
    
                /*** add each option to the dropdown ***/
                $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
            }
    
            /*** close the select ***/
            $dropdown .= '</select>'."\n";
    
            /*** and return the completed dropdown ***/
            return $dropdown;
        }
    
    0 讨论(0)
  • 2021-01-13 14:33

    I was actually just having a similar issue and managed to get the following to work:

    $result = array();
    
    // Get return value
    do {
       while ($row = sqlsrv_fetch_array($query)) {
           // Loop through each result set and add to result array
           $result[] = $row;
       }
    } while (sqlsrv_next_result($query));
    
    print_r($result);
    

    The do-while loop will advance through all results (rather than having to do this manually). It seems that looping through sqlsrv_fetch_array() is essential so I think this is the real answer.

    0 讨论(0)
提交回复
热议问题