我想在我的PHP代码中执行存储过程,这个存储过程有一个IN和一个OUT参数。存储过程是这样的:
USE [phl_pmx]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[PMX_SP_RecreateSynonymsOfSourceDb]
@sourceDb = phl
SELECT 'Return Value' = @return_value
GO
我已经编写了以下代码,但它一直给出错误,他无法执行它,或者他只是不会展示一个东西。
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include_once 'DBC.php';
$link = mssql_connect('server', 'sa', 'password');
if (isset($_POST['StoredProcedure']))
{
mssql_select_db($id,$link);
$a = new TableOutput();
$a->getTables ("PMX_SP_RecreateSynonymsOfSourceDb","phl");
mssql_close($link);
}
elseif (isset($_POST['Value']))
{
$query =mssql_query("UPDATE [phl].[dbo].[PMX_EXDB] set ExtraDb='phl_pmx'");
mssql_close($link);
}
?>
这是它的功能。
<?php
class TableOutput {
function getTables($procname, $parameter) {
$stmt = null;
$data = null;
$vars = null;
$num = null;
$con = mssql_connect('server', 'sa', 'password');
if
(!$con) {
die('Could not connect: ' . mssql_error());
}
mssql_select_db("phl",$con);
$this->setStmt(mssql_init($procname, $con));
mssql_bind($this->getStmt(), '@sourceDb', $parameter, SQLINT2, false, false);
if ($rtn != 0) {
echo ("Errors happened when executing the stored procedure");
}
$exec = mssql_execute($this->getStmt());
$data = array();
$i = 0;
while ($row = mssql_fetch_assoc($exec)) {
$data[++$i] = $row;
}
unset($con);
unset($stmt);
return $data;
}
function setStmt($a_stmt) {
$this->stmt = $a_stmt;
}
function getStmt() {
return $this->stmt;
}
}
?>`
有没有人知道如何更正代码,因为它不断向我显示以下错误:
Warning: mssql_execute(): stored procedure execution failed in /var/www/mssql/management/DBC.php on line 24 Warning: mssql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/www/mssql/management/DBC.php on line 27
答案 0 :(得分:0)
我不知道MSSQL的存储过程,所以我无法分析你的存储过程。如果您的proc返回多个结果集,则需要使用mssql_next_result:
do {
while ($row = mssql_fetch_row($exec)) {
$data[++$i] = $row;
}
} while (mssql_next_result($exec));
也许这有帮助。