服务器详情:
PHP v5.3.5
使用 MySQLi 库客户端 api 版本:mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
MySQL Server v5.5.9
我在 MySQL 中有一个名为 f_get_owner_locations( _in int ) 的存储函数。它构造一个文本变量,保存特定所有者拥有的任何公寓的位置。如果我运行一个
SELECT f_get_owner_locations( 3 );
从 MySQL 命令行,它会做它应该做的事情并返回一行:
+----------------------------+
| f_get_owner_locations( 3 ) |
+----------------------------+
| A-01 |
+----------------------------+
但是,每当我尝试使用 MySQLi 库通过 PHP 运行它时:
$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 语句不涉及函数调用时,它不会失败。即这有效:
$sql = "SELECT `id` FROM `owners`";
$owners = $GLOBALS['db']->fetch( $sql );
但是,一旦我添加了将他们拥有的公寓放入其中的需求(并且此语句也可以通过 MySQL 命令行工作):
$sql = "SELECT `id`, f_get_owner_locations(`id`) FROM `owners`";
$owners = $GLOBALS['db']->fetch( $sql );
它给了我关于在非对象上调用成员函数的错误。
我难住了。在 while 循环之前对 $result 执行 var_dump,在我的 _bindResults 方法中给了我 1 个正确的转储,然后停止并且该错误就在那里。
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 的调用是该选择列表中的第二个字段,因此它没有存储正确的字段计数,尽管它说它需要循环到正确数量的字段。
任何绕过这个小障碍的建议或确认这是 MySQLi 库中的错误或我的绑定代码的问题将不胜感激。
更新:以下代码:
mysql_connect( ... );
mysql_query( "select f_get_owner_locations(3)" );
die( mysql_error() );
给了我这个输出:
FUNCTION f_get_owner_locations does not exist.
我更想知道这是否只是 PHP/MySQLi 的失败而不是我的?
更新 2:
根据要求,用于创建函数的代码:
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 ;
允许进行一些可能会更清洁的重构,但这就是我使用的。