有没有办法在zend框架中调用mysql存储函数(而不是过程)?或者绕过框架使用未过滤的php代码?
在“纯”php应用程序中,以下代码可以工作-使用mysqli db连接(而不是mysql)。但是在zend framework中,此代码不会导致应用程序错误,但也不会将结果绑定到php变量。
如果您使用Zend_DB适配器,则无法找到将函数结果绑定到变量的方法(从而得到答案-没有等效于“Bind_Result”的Zend_DB方法)。
我已经看了前面的问题,但它们集中在存储过程(例如How can I use a stored procedure in a MySql database with Zend Framework?)。
谢谢。
更新:自编写本文以来,我发现在mamp上使用mysql可以很好地工作,但在mac上的zend server ce上使用mysql不行。
MySQL存储函数:

CREATE FUNCTION GetUserAccountType(
inUsername  VARCHAR(50)
) RETURNS INT(3)
BEGIN
    SELECT AccountType_i INTO @retVal FROM UserTable WHERE Username_vc = inUsername;
    RETURN @retVal;
END;
GO

调用mysql函数的php代码:
// open mysqli connection with db
$my_db = new mysqli($db_hostname, $db_username, $db_password, $db_database);

//  Prepare MySQL function ($userName is a string)

$stmt=$my_db->prepare("SELECT GetUserAccountType(?)") or die($my_db->error);
$stmt->bind_param('s', $userName) or die($stmt->error);
$stmt->execute() or die($stmt->error);
$stmt->bind_result($result);
$stmt->fetch();

// (in php the $result variable  is now set as the result of the MySQL function, @retVal.
// In Zend framework, the $result variable is set to FALSE)
return $result;

最佳答案

我刚刚用你的示例存储函数进行了测试。我运行了下面的代码,运行得很好。我在MacOSX上使用PHP5.3.6,连接到CentOS6Linux上的PerconaServer5.5.18实例。

<?php

set_include_path(get_include_path() . ":ZendFramework-1.11.10/library");

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$db = Zend_Db::factory('pdo_mysql', ...);

// test that I can read the table directly
print_r($db->query("SELECT * FROM UserTable")->fetchAll());

// test that I can fetch from a stored function
$stmt = $db->prepare("SELECT GetUserAccountType(?)");
$stmt->execute(array("default"));
print_r($stmt->fetchAll());

注意,结果集使用完整表达式作为键返回。
所以我的打印输出如下:
Array
(
    [0] => Array
        (
            [GetUserAccountType('default')] => 42
        )

)

如果希望使用更常规的键,则应为列提供别名。
例如:
$stmt = $db->prepare("SELECT GetUserAccountType(?) AS AccountType_i");

10-05 20:29
查看更多