FreeBSD下使用SQL Server2005(2008)需要使用FreeTDS。
安装FreeTSD后,使用PHP语言进行测试。在使用mssql_execute调用存储过程时出现如下错误:
Warning: mssql_execute():stored procedure execution failed in...
语法如下:
...
$sp1=mssql_init('uspUserGetPoints_byID',$t_dbconn);
$userid=11360;
$account='cp';
$points=0;//output
mssql_bind($sp1,'@UserID ',$userid,SQLINT2);
mssql_bind($sp1,'@Account ',$account,SQLCHAR,false,false,2);
mssql_bind($sp1,'@Points',$points,SQLINT2,TRUE);
$result=mssql_execute($sp1,true);
...
注:uspUserGetPoints_byID是存储过程名,包括2个输入参数1个输出参数,有返回值
查找资料据说需要修改freetds.conf,增加如下内容:
[open119]
host = xxx.xxx.xxx.xxx
port = 1433
tds version = 8.0
修改后问题依旧...
最后只好尝试另一种调用存储过程的方法,语法如下:
...
$query = "declare @Result INT\n";
$query .= "declare @Points INT\n";
$query .= "exec @Result=uspUserGetPoints_byID $userid,'$account',@Points OUTPUT\n";
$query .= "select @Result as Result,@Points as Points\n";
//执行存储过程
$result=mssql_query($query) or die("Error");
$row=mssql_fetch_array($result);
echo $row['Result'];
echo '<br>';
echo $row['Points'];
...
运行成功。
附连接数据库代码:
$DatabaseHost='172.54.88.52:1433';
$DatabaseName='testDB';
$DatabaseUser='test1';
$DatabasePwd='123456';
$t_dbconn=mssql_connect($DatabaseHost,$DatabaseUser,$DatabasePwd);
if(!$t_dbconn)
{
$tr[0][0]=1;
echo $tr[0][0];
exit();//连接数据库失败
}
//echo "connect ok!";
$dbobject=mssql_select_db('ompay2',$t_dbconn);
if(!$dbobject)
{
echo 'db is not ok';
exit();
}
附SQL使用的参数定义代码如下:
//if(!defined('dbMSSQL_Types'))
//{
// define('dbMSSqlTypes',1);
// $MSSQL_types[127] = SQLINT4; /* bigint*/
// $MSSQL_types[104] = SQLBIT; /* bit*/
// $MSSQL_types[175] = SQLCHAR; /* char*/
// $MSSQL_types[56] = SQLINT2; /* int*/
// $MSSQL_types[52] = SQLINT2; /* smallint*/
// $MSSQL_types[35] = SQLTEXT; /* text*/
// $MSSQL_types[48] = SQLINT1; /* tinyint*/
// $MSSQL_types[167] = SQLVARCHAR; /* varchar*/
// $MSSQL_types[62] = SQLFLT8; /* float*/
// $MSSQL_types[173] = SQLVARCHAR; /* binary*/ // Adaptation
// $MSSQL_types[61] = SQLINT4; /* datetime*/ // Adaptation
// $MSSQL_types[106] = SQLFLT8; /* decimal*/ // Adaptation
// $MSSQL_types[34] = SQLVARCHAR; /* image*/ // Adaptation
// $MSSQL_types[60] = SQLFLT8; /* money*/ // Adaptation
// $MSSQL_types[239] = SQLCHAR; /* nchar*/ // Adaptation
// $MSSQL_types[99] = SQLTEXT; /* ntext*/ // Adaptation
// $MSSQL_types[108] = SQLFLT8; /* numeric*/ // Adaptation
// $MSSQL_types[231] = SQLVARCHAR; /* nvarchar*/ // Adaptation
// $MSSQL_types[59] = SQLFLT8; /* real*/ // Adaptation
// $MSSQL_types[58] = SQLINT4; /* smalldatetime*/ // Adaptation
// $MSSQL_types[122] = SQLFLT8; /* smallmoney*/ // Adaptation
// $MSSQL_types[98] = SQLVARCHAR; /* sql_variant*/ // Adaptation
// $MSSQL_types[189] = SQLINT4; /* timestamp*/ // Adaptation
// $MSSQL_types[165] = SQLVARCHAR; /* varbinary*/ // Adaptation
//}