在PHP中使用ADODB调用存储过程

问题描述:

我找不到有关如何在PHP中使用ADODB调用存储过程的适当文档.有人可以帮忙吗?

I couldn't find a proper documentation on how to call a stored procedure using ADODB in PHP. Can someone help please?

这就是我现在拥有的,我觉得这是一种肮脏的方法(或者不是吗?):

This is what I have now and I feel it's a dirty approach (or not?):

$stmt = "CALL LocFillData('foo', 'bar', @nullcount, @totalcount)";
$rsstmt = "SELECT @nullcount, @totalcount";
$rs = $db->Execute($stmt);
$rsstmt = $db->Execute($rsstmt);
var_dump($rsstmt);

除了对CALL语句进行硬编码之外,还有什么方法可以对多数据库标准进行编码?

Instead of hardcoding the CALL statement, is there any way to code it for multi-database standard?

我按照 ADODB手册中的建议尝试了此代码:>

I tried this code as suggested in the ADODB Manual:

$dbname = DB_DATABASE;
$tbname = TABLE_CONTACT_LOCATIONS;

$stmt = $db->PrepareSP("BEGIN; adodb.LocFillData(:dbname, :tbname, :nullcount, :totalcount); END;");
$db->InParameter($stmt,$dbname,'dbname');
$db->InParameter($stmt,$tbname,'tbname');
$db->OutParameter($stmt,$nullcount,'nullcount');
$db->OutParameter($stmt,$totalcount,'totalcount');
$ok = $db->Execute($stmt) or die($db->ErrorMsg());

echo "<pre>";
var_dump($ok);
echo "</pre>";

但是结果是:

object(ADORecordSet_empty)#15 (6) {
  ["dataProvider"]=>
  string(5) "empty"
  ["databaseType"]=>
  bool(false)
  ["EOF"]=>
  bool(true)
  ["_numOfRows"]=>
  int(0)
  ["fields"]=>
  bool(false)
  ["connection"]=>
  bool(false)
}

可能是什么问题?该文档是针对Oracle的,而我正在使用MySQL.但是我相信所有数据库的ADODB方法都是相同的.

What could be the problem? The documentation was for Oracle and I am using MySQL. But I believe ADODB methods are same for all DBs.

谢谢您的帮助.

确定.我自己找到了答案.在MySQL的ADODB中,没有干净的方法可以做到这一点.可以使用PrepareSP()方法,但不能使用InParameterOutParameter方法.

OK. I found the answer myself. There is no clean way to do that in ADODB for MySQL. The PrepareSP() method can be used, but not the InParameter or the OutParameter methods.

ADODB文档说:

InParameter()是包装函数 调用Parameter()与 $ isOutput = false.这样的好处 功能是 自我记录,因为 $ isOutput参数不再 需要. 仅适用于mssql和oci8 当前.

InParameter() is a wrapper function that calls Parameter() with $isOutput=false. The advantage of this function is that it is self-documenting, because the $isOutput parameter is no longer needed. Only for mssql and oci8 currently.

OutParameter()是包装函数 调用Parameter()与 $ isOutput = true.这样的好处 功能是 自我记录,因为 $ isOutput参数不再 需要. 仅适用于mssql和oci8 当前.

OutParameter() is a wrapper function that calls Parameter() with $isOutput=true. The advantage of this function is that it is self-documenting, because the $isOutput parameter is no longer needed. Only for mssql and oci8 currently.