Note that there are some explanatory texts on larger screens.

plurals
  1. POFatal Error while calling MySQL stored function from PHP using MySQLi
    primarykey
    data
    text
    <p>Server details:<br> PHP v5.3.5<br> Using MySQLi library client api version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $<br> MySQL Server v5.5.9 </p> <p>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 </p> <pre><code>SELECT f_get_owner_locations( 3 ); </code></pre> <p>from the MySQL command line, it does what it's supposed to do and returns one row:</p> <pre><code>+----------------------------+ | f_get_owner_locations( 3 ) | +----------------------------+ | A-01 | +----------------------------+ </code></pre> <p>However, whenever I try and run it through PHP using the MySQLi library as such:</p> <pre><code>$sql = "SELECT f_get_owner_locations( 3 )"; $location = $GLOBALS['db']-&gt;fetch( $sql ); </code></pre> <p>I get this error: </p> <pre><code>Fatal error: Call to a member function fetch_field() on a non-object in ~/kernel/Database.php on line 328 </code></pre> <p>That line refers to this:</p> <pre><code>/** * 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-&gt;result_metadata( ); //Loop through the fields and get a reference to each. while( $column = $result-&gt;fetch_field() ) //&lt;=&lt;=&lt;=LINE 328 $bind[] = &amp;$results[$column-&gt;name]; //Do the actual binding. call_user_func_array( array( $statement, 'bind_result'), $bind ); //Free the memory since we already have the result. $result-&gt;free_result(); return $results; } //_bindResult </code></pre> <p>Keep in mind it doesn't fail when the SQL statement doesn't involve a function call. i.e. This works:</p> <pre><code>$sql = "SELECT `id` FROM `owners`"; $owners = $GLOBALS['db']-&gt;fetch( $sql ); </code></pre> <p>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):</p> <pre><code>$sql = "SELECT `id`, f_get_owner_locations(`id`) FROM `owners`"; $owners = $GLOBALS['db']-&gt;fetch( $sql ); </code></pre> <p>It gives me that error about call to a member function on a non-object.</p> <p>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.</p> <pre><code>object(mysqli_result)#11 (5) { ["current_field"]=&gt; int(1) ["field_count"]=&gt; int(1) ["lengths"]=&gt; NULL ["num_rows"]=&gt; int(0) ["type"]=&gt; int(1) } </code></pre> <p>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.</p> <p>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.</p> <p>UPDATE: The following code:</p> <pre><code>mysql_connect( ... ); mysql_query( "select f_get_owner_locations(3)" ); die( mysql_error() ); </code></pre> <p>Gave me this output:</p> <pre><code>FUNCTION f_get_owner_locations does not exist. </code></pre> <p>I'm more wondering if this is just a failure on PHP/MySQLi's part than mine?</p> <p>UPDATE 2:<br> As requested, the code used to create the function: </p> <pre><code>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 ; </code></pre> <p>Granted with a bit of refactoring that could could be a small bit cleaner, but that's what I used.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload