Search code examples
phpmysqlprepared-statementstored-functions

PHP Prepared Statement Failing When Calling MySQL Function


I've been using prepared statements for a good while now with no issues but today when I tried to call a MySQL function from a prepared statement I'm getting the following:

Fatal error: Call to a member function fetch_array() on boolean in DB.php on line 336

This is the code I'm using and there is nothing different from my usual SELECT, UPDATE or DELETE querys, I also have no issues calling procedures, granted none of my procedures are returning any value.

$sql = "SELECT FN_MAINTAIN_ASSET(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) AS assetId;";

try {
    $conn = $this->open();

    $stmt = $conn->prepare($sql);
    if($stmt === false) {
        trigger_error(sprintf($txt["error_sql"], $conn->error), E_USER_ERROR);
    }

    $stmt->bind_param('iiiisssbbiiiis', $modify, $type, $category, $year, $title, $description, $imageFilename, $imageMain, $imageThumbnail, $membersOnlyView, $privateView, $status, $memberId, $createdIp);
    $stmt->execute();

    $rs = $stmt->get_result();
    $result = $rs->fetch_array(MYSQL_ASSOC);  // Line 336
    $rs->free();

    $stmt->close();
    $this->close($conn);
} catch (Exception $e) {
    $logObj->error($e->getMessage());
}

$rs is empty just before line 336, no error messages from $stmteither.

If anyone has any suggestions or ideas, greatly appreciated.


Solution

  • From the manual for get_result:

    Return Values

    Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure. The mysqli_errno() function can be used to distinguish between the two types of failure.

    If your statement is returning a boolean, it failed. Check the error message from $stmt->error, and correct whatever's wrong.