I want to execute a stored procedure into my PHP code, this stored procedure has an IN and an OUT parameter. The stored procedure is this:
USE [phl_pmx]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[PMX_SP_RecreateSynonymsOfSourceDb]
@sourceDb = phl
SELECT 'Return Value' = @return_value
GO
And I already wrote the following code, but it keeps giving errors that he can't execute it, or he just won't show a thing.
<?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);
}
?>
This is the function for it.
<?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-&开发者_如何学Gogt;stmt = $a_stmt;
}
function getStmt() {
return $this->stmt;
}
}
?>`
Does anyone know how to correct the code, because it keeps showing me the following error:
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
I don't know MSSQL stored procedures, so I can't analyse your stored procedure. If your proc is returning multiple resultsets you need to use mssql_next_result:
do {
while ($row = mssql_fetch_row($exec)) {
$data[++$i] = $row;
}
} while (mssql_next_result($exec));
Maybe this helps.
精彩评论