开发者

Fatal Error while calling MySQL stored function from PHP using MySQLi

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

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 );  

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

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

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 );

I get this error:

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

That line refers to this:

/**
 * 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_f开发者_JAVA百科unc_array( array( $statement, 'bind_result'), $bind );

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

    return $results;
} //_bindResult

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 );

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.

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)
}

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.

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.

UPDATE: The following code:

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

Gave me this output:

FUNCTION f_get_owner_locations does not exist.

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

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.


MySQL stored procedures can return multiple result sets, where the last result set is the actual data you're interested in. One of the few things that mysqli does correctly is support multiple result sets.

Try checking mysqli::more_results. Also check the disclaimers on the manual page for mysqli::store_result about how mysqli handles multiple result sets wherein one might not have data. You'll end up working with the mysqli_result class instead of mysqli_stmt.


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

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.

Oh the joys of IT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜