gpt4 book ai didi

php pdo mssql 声明插入选择

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:54 25 4
gpt4 key购买 nike

我正在尝试使用 PHP PDO:SQLSRV 或 PHP SQLSRV 访问 MS SQL 服务器。

对于 PHP PDO:SQLSRV,我使用以下代码:

<?php

try {
$db = new PDO("sqlsrv:Server=sql_server;Database=database", "username", "password");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

$test = $db->prepare('declare @t2 code.instancereferencestable;
insert into @t2 select top 10 [Instance Reference] from imported.instance where [Instance Type] = \'CENAS\' and [Instance Reference] = \'CENAS.8000\' order by [Instance (Id)] desc;
select * from transformed.attribute_instance_extended_from_references(@t2)');
$test->execute();
$test->fetch(PDO::FETCH_OBJ);

echo "Connected!". "\n";
}
catch(Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}

?>

对于 PHP SQLSRV,我使用以下代码:

<?php

$serverName = "sql_server";
$connectionOptions = array(
"Database" => "database",
"Uid" => "username",
"PWD" => "password"
);

$conn = sqlsrv_connect($serverName, $connectionOptions);
if ( $conn ) {
echo "Connection established.".PHP_EOL;

$sql = ("declare @t2 code.instancereferencestable;
insert into @t2 select top 10 [Instance Reference] from imported.instance where [Instance Type] = 'CENAS' and [Instance Reference] = 'CENAS.8000' order by [Instance (Id)] desc;
select * from transformed.attribute_instance_extended_from_references(@t2);");

$stmt = sqlsrv_query($conn, $sql);
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
} else {
$row = sqlsrv_fetch_array($stmt);
print_r ($row);
}

} else {
echo "Connection could not be established.".PHP_EOL;
die( print_r( sqlsrv_errors(), true)).PHP_EOL;
}

?>

我从 PHP PDO:SQLSRV 得到的输出是:错误:SQLSTATE[IMSSP]:查询的事件结果不包含任何字段。

并且从 PHP SQLSRV 我没有输出。

我直接在 SQL Server Management Studio 中运行相同的 SQL 代码,并且有输出。

谁能帮忙!?提前致谢!

最佳答案

一个简单的解决方案可能是创建一个存储过程并调用它。

您也可以尝试使用 php_mssql.dll 模块。此处信息:http://php.net/manual/es/book.mssql.php

关于php pdo mssql 声明插入选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com