首页 > 解决方案 > mysql中的SQLSRV绑定参数

问题描述

我正在尝试将此 mysql 代码转换为使用 sqlsrv

$planeId = $_GET["pid"];

$conn = OpenCon();

$sql = "SELECT id, pid, fullname, tat, date, engine1, engine2, engine3, engine4 FROM oil WHERE pid = ? order by date desc";
$stmnt = $conn->prepare($sql);
$stmnt->bind_param("s", $planeId);
$stmnt->bind_result($id, $pid, $fullname, $tat, $date, $engine1, $engine2, $engine3, $engine4);
$stmnt->execute();
$theRows = Array();
while ( $stmnt->fetch() )
{
    $aRow['id'] = "$id";
    $aRow['pid'] = "$pid";
    $aRow['fullname'] = $fullname;
    $aRow['tat'] = $tat;
    $aRow['date'] = $date;
    $aRow['engine1'] = $engine1;
    $aRow['engine2'] = $engine2;
    $aRow['engine3'] = $engine3;
    $aRow['engine4'] = $engine4;

    $theRows[] = $aRow;
}

$stmnt->close();

echo json_encode($theRows);

CloseCon($conn);

这是我到目前为止所做的,但我错过了 bind-param 函数,不知道如何实现它。因为输出一直像这样

[{"id":"","pid":"","fullname":null,"tat":null,"date":null,"engine1":null,"engine2":null,"engine3":null,"engine4":null}]

即使我知道 Microsoft DB 中有一个条目

$planeId = $_GET["pid"];
$theRows = Array();
$conn = OpenCon();

$query = "SELECT id, pid, fullname, tat, date, engine1, engine2, engine3, engine4 FROM oil WHERE pid = ? order by date desc";

//$stmnt = $conn->prepare($query);
$stmnt = sqlsrv_prepare($conn, $query, array(&$planeId));

if (sqlsrv_execute($stmnt) === false){
    die( print_r( sqlsrv_errors(), true));
}
else{
    while ( sqlsrv_fetch($stmnt) )
    {
        $aRow['id'] = "$id";
        $aRow['pid'] = "$pid";
        $aRow['fullname'] = $fullname;
        $aRow['tat'] = $tat;
        $aRow['date'] = $date;
        $aRow['engine1'] = $engine1;
        $aRow['engine2'] = $engine2;
        $aRow['engine3'] = $engine3;
        $aRow['engine4'] = $engine4;

        $theRows[] = $aRow;
    }
    echo json_encode($theRows);
}
CloseCon($conn);

标签: phpmysqlsqlsrv

解决方案


推荐阅读