使用MySQLi从PHP调用MySQL存储函数时发生致命错误 [英] Fatal Error while calling MySQL stored function from PHP using MySQLi

查看:243
本文介绍了使用MySQLi从PHP调用MySQL存储函数时发生致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器详细信息:
PHP v5.3.5
使用MySQLi库客户端api版本:mysqlnd 5.0.7-dev-091210-$修订版:304625 $
MySQL服务器v5.5.9

Server details:
PHP v5.3.5
Using MySQLi library client api version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
MySQL Server v5.5.9

我在MySQL中有一个称为f_get_owner_locations(_in int)的存储函数.它构造一个文本变量,以保存特定所有者拥有的公寓的位置. 如果我运行

I have a stored function in MySQL called f_get_owner_locations( _in int ). It constructs a text variable that holds the locations of whichever condos a specific owner owns. If I run a

SELECT f_get_owner_locations( 3 );  

从MySQL命令行执行它应该执行的操作,并返回一行:

from the MySQL command line, it does what it's supposed to do and returns one row:

+----------------------------+
| f_get_owner_locations( 3 ) |
+----------------------------+
| A-01                       |
+----------------------------+

但是,每当我尝试使用MySQLi库这样通过PHP运行它时:

However, whenever I try and run it through PHP using the MySQLi library as such:

$sql = "SELECT f_get_owner_locations( 3 )";
$location = $GLOBALS['db']->fetch( $sql );

我收到此错误:

Fatal error: Call to a member function fetch_field() on a non-object 
in ~/kernel/Database.php on line 328

该行涉及到此:

/**
 * Binds results from a returning SQL statement to an array we can 
 * loop through.
 * 
 * @param   $statement  Statement object we're binding from.
 * @return  Array of values being returned.
 * @since   0.1
 */
private final function _bindResult( $statement )
{
    $results = NULL;
    $bind = array( );

    //Get the result set, so we can loop through the fields.
    $result = $statement->result_metadata( );
    //Loop through the fields and get a reference to each.
    while( $column = $result->fetch_field() ) //<=<=<=LINE 328
        $bind[] = &$results[$column->name];
    //Do the actual binding.
    call_user_func_array( array( $statement, 'bind_result'), $bind );

    //Free the memory since we already have the result.
    $result->free_result();

    return $results;
} //_bindResult

请记住,当SQL语句不涉及函数调用时,它不会失败.即,这有效:

Keep in mind it doesn't fail when the SQL statement doesn't involve a function call. i.e. This works:

$sql = "SELECT `id` FROM `owners`";
$owners = $GLOBALS['db']->fetch( $sql );

但是,一旦我添加了将它们拥有的公寓的需求(并且该语句也可以通过MySQL命令行运行),就可以了:

But as soon as I add in the need to get the condos they own into it (and this statement works through the MySQL command line as well):

$sql = "SELECT `id`, f_get_owner_locations(`id`) FROM `owners`";
$owners = $GLOBALS['db']->fetch( $sql );

这给了我关于在非对象上调用成员函数的错误.

It gives me that error about call to a member function on a non-object.

我很困惑.在while循环之前在$ result上执行var_dump,在我的_bindResults方法中为我提供了1个正确的转储,然后停止并出现了该错误.

I'm stumped. Doing a var_dump on $result right before the while loops, in my _bindResults method gives me 1 proper dump, and then stops and that error is there.

object(mysqli_result)#11 (5) {
  ["current_field"]=>
  int(1)
  ["field_count"]=>
  int(1)
  ["lengths"]=>
  NULL
  ["num_rows"]=>
  int(0)
  ["type"]=>
  int(1)
}

注意:对f_get_owner_locations的调用是该选择列表上的第二个字段,因此尽管它需要循环到正确的字段数量,但它并未存储正确的字段数.

Note: The call to f_get_owner_locations is the 2nd field on that select list, so it's not storing the right field count, despite saying that it needs to loop to the correct amount of fields.

任何绕开这个小障碍的建议,或者确认这是MySQLi库中的错误或我的绑定代码有问题的建议,将不胜感激.

Any suggestions to get around this little road block or a confirmation that this is a bug within the MySQLi library or a problem with my binding code would be much appreciated.

更新: 以下代码:

mysql_connect( ... );
mysql_query( "select f_get_owner_locations(3)" );
die( mysql_error() );

给我这个输出:

FUNCTION f_get_owner_locations does not exist.

我更想知道这是否只是PHP/MySQLi方面的失败而不是我的失败?

I'm more wondering if this is just a failure on PHP/MySQLi's part than mine?

更新2:
根据要求,用于创建函数的代码:

UPDATE 2:
As requested, the code used to create the function:

drop function if exists f_get_owner_locations;
delimiter |
create function f_get_owner_locations( _in int )
returns text deterministic
begin
    declare _output text default "";
    declare _location varchar(255);
    declare _count int default 0;
    declare _done int default 0;
    declare _cursor cursor for 
        select
            condos.location
        from
            owners left join
            condo_owners on owners.id = condo_owners.owner left join
            condos on condo_owners.condo = condos.id
        where
            owners.id = _in;
    declare continue handler for not found set _done = 1;

    open _cursor;

    repeat
        fetch _cursor into _location;
        set_count = _count + 1;
        set_output = concat( _output, ", ", _location );
    until _done end repeat;

    set _count = _count - 1;

    close _cursor;

    set _output = trim( leading ", " from _output );
    set _output = substring( _output from 1 for (_count * 6) );
    set _output = trim( trailing ", " from _output );
    return _output;
end;|
delimiter ;

被允许进行一些重构,这可能会更干净一些,但这就是我使用的.

Granted with a bit of refactoring that could could be a small bit cleaner, but that's what I used.

推荐答案

因此,在进行了更多测试之后,我发现它实际上并不是MySQLi库或代码中的错误.

So, after more testing and what not, I find that it's not actually a bug in the MySQLi libraries or in my code.

解决方案?向数据库用户授予MySQL中数据库的执行"权限.当我以root身份而不是脚本使用的实际用户身份登录时,我编写并测试了SQL语句和函数.

The Solution? Give the database user the "Execute" permission for the database in MySQL. I wrote and tested the SQL statements and functions while I was logged in as root, not the actual user that the script was using.

哦,IT的乐趣.

这篇关于使用MySQLi从PHP调用MySQL存储函数时发生致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆