代码之家  ›  专栏  ›  技术社区  ›  Rebe

为函数中传递的参数赋值

  •  1
  • Rebe  · 技术社区  · 7 年前

    sqlsrv_prepare

    $getNotesSQL = "SELECT pat_id as PAT_ID, note_id as NOTE_ID, CONVERT(char(10), UPDATE_DATE, 120) as UPDATE_DATE ";
    $getNotesSQL .= "FROM CLARITY.dbo.HNO_INFO";
    $getNotesSQL .= " WHERE ip_note_type_c = ? ";
    $getNotesSQL .= " AND  (UPDATE_DATE >= ? AND UPDATE_DATE <= ?)";
    
    if (!$getNotes = sqlsrv_prepare($clarity, $getNotesSQL, array(&$noteType, &$startDate, &$endDate))) {
        echo "getNotesSQL couldn't be prepared\n";
        die(print_r(sqlsrv_errors(), true));
    }
    
    $note_type = strval(1);
    $start_date = "2017-05-29";
    $end_date = "2017-07-11";
    
    /**
    $noteType = strval(1);
    $startDate = "2017-07-01";
    $endDate = "2017-07-11";
    */
    
    function getNotes($getNotes, $note_type, $start_date, $end_date) {
    
        $noteType = $note_type;
        $startDate = $start_date;
        $endDate = $end_date;
    
        if (!sqlsrv_execute($getNotes)) {`enter code here`
            echo "getNotes Couldn't be executed\n";
            die(print_r(sqlsrv_errors(), true));
        }
    
        $noteArray = array();
        $iii=0;
        while ($row = sqlsrv_fetch_array($getNotes, SQLSRV_FETCH_ASSOC)) {
       //     print_r($row);
            $noteArray[$iii] = $row;
            $iii++;
        }
    
        echo "In getNote Function  iii: (" . $iii .")\n";
        print_r($noteArray);
        return $noteArray;
    }
    
    
    
    $fetchedNotes = getNotes($getNotes, $note_type, $start_date, $end_date);
    
    print_r($fetchedNotes);
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   ImClarky    7 年前

    我不完全确定它背后的原因-我认为可能与范围有关-但您也需要通过引用将查询参数变量传递到函数中。

    比如这样:

    function getNotes($getNotes, $note_type, $start_date, $end_date, &$noteType, &$startDate, &$endDate){
        //perform query
    }
    

    function getNotes($getNotes, $values, $params){
        foreach($params as $key => &$param){
            $param = $values[$key];
        }
    
        // sqlsrv_execute
    }
    
    $values = [$note_type, $start_date, $end_date];
    $params = [&$noteType, &$startDate, &$endDate];
    
    $fetchedNotes = getNotes($getNotes, $values, $params);
    

    我在我的用户表上尝试了类似的东西来测试它,似乎效果不错。