将TVP与codeigniter框架和MSSQL结合使用

qyuhtwio  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(97)

我找不到任何关于将TVP与codeigniter & MSSQL一起使用的文档,需要调用一个存储过程,将int列表作为单个参数。
第一个
谢谢

j5fpnvbx

j5fpnvbx1#

我已经绕过了codeigniter并遵循了Microsoft's guide,php现在看起来像这样:

/* Specify the server and connection string attributes. */
$serverName = "localhost";

/* Get UID and PWD from application-specific files.  */
$uid = "username";//file_get_contents("C:\AppData\uid.txt");
$pwd = "password";//file_get_contents("C:\AppData\pwd.txt");
$connectionInfo = array(
    "UID" => $uid,
    "PWD" => $pwd,
    "Database" => "databaseName"
);

/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
    echo "Unable to connect.</br>";
    die(print_r(sqlsrv_errors(), true));
}

/* Set the parameters. */
$int_array = array(1, 2, 3);
$tvpType = 'TypeInteger';
$tvpInput = array($tvpType => $int_array);

$params = array(array($tvpInput, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_TABLE, SQLSRV_SQLTYPE_TABLE));

/* Query SQL Server. */
$tsql = "{call spDoStuff(?)}";
$stmt = sqlsrv_query($conn, $tsql, $params);
if ($stmt === false) {
    echo "Error in executing query.</br>";
    die(print_r(sqlsrv_errors(), true));
}        
        
/* Retrieve each row as an associative array and display the results.*/  
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))  
{  
    echo $row['LastName'].", ".$row['FirstName']."\n";
}  

/* Free statement and connection resources. */
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

现在可以迭代$row。

相关问题