我的宿主提供程序安装了PHP 5.3而没有mysqlnd,因此当我调用mysqli_stmt_get_result()时,它返回一个错误
在不安装mysqlnd的情况下,是否有任何解决方法来实现相同的功能?
我不想更改100个函数,如果有更简单的方法,请提供帮助:)

最佳答案

在mysqlnd不可用的服务器上,我遇到了很多麻烦,而且头疼不已。
如果您没有安装/加载mysqlnd,那么在尝试调用mysqli_stmt_get_result()时将得到一个未定义的引用。
我写了我自己的mysqli_stmt_get_result()和一个mysqli_result_fetch_array()来配合它。

<?php
class iimysqli_result
{
    public $stmt, $nCols;
}

function iimysqli_stmt_get_result($stmt)
{
    /**    EXPLANATION:
     * We are creating a fake "result" structure to enable us to have
     * source-level equivalent syntax to a query executed via
     * mysqli_query().
     *
     *    $stmt = mysqli_prepare($conn, "");
     *    mysqli_bind_param($stmt, "types", ...);
     *
     *    $param1 = 0;
     *    $param2 = 'foo';
     *    $param3 = 'bar';
     *    mysqli_execute($stmt);
     *    $result _mysqli_stmt_get_result($stmt);
     *        [ $arr = _mysqli_result_fetch_array($result);
     *            || $assoc = _mysqli_result_fetch_assoc($result); ]
     *    mysqli_stmt_close($stmt);
     *    mysqli_close($conn);
     *
     * At the source level, there is no difference between this and mysqlnd.
     **/
    $metadata = mysqli_stmt_result_metadata($stmt);
    $ret = new iimysqli_result;
    if (!$ret) return NULL;

    $ret->nCols = mysqli_num_fields($metadata);
    $ret->stmt = $stmt;

    mysqli_free_result($metadata);
    return $ret;
}

function iimysqli_result_fetch_array(&$result)
{
    $ret = array();
    $code = "return mysqli_stmt_bind_result(\$result->stmt ";

    for ($i=0; $i<$result->nCols; $i++)
    {
        $ret[$i] = NULL;
        $code .= ", \$ret['" .$i ."']";
    };

    $code .= ");";
    if (!eval($code)) { return NULL; };

    // This should advance the "$stmt" cursor.
    if (!mysqli_stmt_fetch($result->stmt)) { return NULL; };

    // Return the array we built.
    return $ret;
}
?>

关于php - mysqlnd缺少驱动程序的解决方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31562359/

10-11 02:35