PHP mysqli get_result不起作用,但bind_result有效

时间:2016-02-18 20:07:03

标签: php mysql mysqli

我创建了一个函数,它返回通过执行给定查询返回的所有行。它适用于所有非连接查询,但只要我将此函数称为" SELECT * FROM A,B ..."一种加入查询,它给了我错误:

  

例外' ErrorException'消息mysqli_stmt :: bind_result():绑定变量的数量与预备语句中的字段数不匹配'

所以我浏览了stackoverflow,开始使用get_result(..)代替bind_result(..)

我之前使用bind_result(...)的代码如下:

function fetchRows($mysqli, $qry, $bindStr, $bindVarArr) {
if ($stmt = $mysqli->prepare($qry)) {

    $rows = NULL;

    if($bindVarArr != NULL && sizeof($bindVarArr) > 0) {

        array_unshift($bindVarArr, $bindStr);
        // $commaList = implode(', ', $bindVarArr);
        // $stmt->bind_param($bindStr, $commaList);
        call_user_func_array(array($stmt,'bind_param'), makeValuesReferenced($bindVarArr));
    }
    // Execute the prepared query.
    if (! $stmt->execute()) {
        throw new DBException(DBException::STMT_ERR_MSG, DBException::STMT_ERR_CODE, $qry);
    }

    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows;

    if ($num_rows <= 0) {
        // No rows returned, may be a valid case

    } else {

        // bind results to named array
        $meta = $stmt->result_metadata();
        $fields = $meta->fetch_fields();
        foreach($fields as $field) {
            $result[$field->name] = "";
            $resultArray[$field->name] = &$result[$field->name];
        }

        call_user_func_array(array($stmt, 'bind_result'), $resultArray);

        // create object of results and array of objects
        while($stmt->fetch()) {
                        /*
                         * If no result object is specified then create a new stdclass object
                         */
                        $resultObject = new stdClass();

                        foreach ($resultArray as $key => $value) {
                                $resultObject->$key = $value;
                        }
                        $rows[] = $resultObject;
        }
    }

    $stmt->close();

    return $rows;
} else {
    throw new DBException(DBException::STMT_ERR_MSG, DBException::STMT_ERR_CODE, $qry);
}

}

我使用get_result(...)的新代码如下:

function fetchRows($mysqli, $qry, $bindStr, $bindVarArr) {
if ($stmt = $mysqli->prepare($qry)) {

    $rows = NULL;

    if($bindVarArr != NULL && sizeof($bindVarArr) > 0) {

        array_unshift($bindVarArr, $bindStr);
        // $commaList = implode(', ', $bindVarArr);
        // $stmt->bind_param($bindStr, $commaList);
        call_user_func_array(array($stmt,'bind_param'), makeValuesReferenced($bindVarArr));
    }
    // Execute the prepared query.
    if (! $stmt->execute()) {
        throw new DBException(DBException::STMT_ERR_MSG, DBException::STMT_ERR_CODE, $qry);
    }

    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows;

    if ($num_rows <= 0) {
        // No rows returned, may be a valid case

    } else {

        $result = $stmt->get_result();
        while ($resultArray = $result->fetch_assoc()) {
                        /*
                         * If no result object is specified then create a new stdclass object
                         */
                        $resultObject = new stdClass();

                        foreach ($resultArray as $key => $value) {
                                $resultObject->$key = $value;
                        }
                        $rows[] = $resultObject;
        }
    }

    $stmt->close();

    return $rows;
} else {
    throw new DBException(DBException::STMT_ERR_MSG, DBException::STMT_ERR_CODE, $qry);
}

}

但现在,它给出了一个错误如下(在所有可能的查询中,使用以前的代码):

  

PHP致命错误:在布尔值

上调用成员函数fetch_assoc()

它清楚地表明声明的get_result可能已经失败,但可能是什么问题?相同的查询完全正常,否则使用bind_result的先前代码。

我使用PHP 5.5,如果有帮助

0 个答案:

没有答案
相关问题