简体   繁体   English

mysqlnd缺少驱动程序的解决方法

[英]workaround for mysqlnd missing driver

My hosting provider has PHP 5.3 without mysqlnd installed, and thus it returns an error when I call mysqli_stmt_get_result() 我的托管服务提供商安装了​​未安装mysqlnd的PHP 5.3,因此在我调用mysqli_stmt_get_result()时返回错误

is there any workaround to achieve the same functionality without having to install mysqlnd? 有什么解决方法可以在无需安装mysqlnd的情况下实现相同的功能?

I don't want to change 100's of functions, if there's an easier method, any help will be appreciated :) 我不想更改100的功能,如果有更简单的方法,将不胜感激:)

I went through a lot of trouble on a server where mysqlnd wasn't available, and had a lot of headaches. 我在无法使用mysqlnd的服务器上遇到了很多麻烦,并且头疼很多。

If you don't have mysqlnd installed/loaded whatever, you will get an undefined reference when trying to call mysqli_stmt_get_result() . 如果您没有安装/加载mysqlnd,则在尝试调用mysqli_stmt_get_result()时会得到未定义的引用。

I wrote my own mysqli_stmt_get_result() and a mysqli_result_fetch_array() to go with it. 我编写了自己的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;
}
?>

This related question has another workaround to get data without installing mysqlnd. 这个相关问题还有另一个解决方法,无需安装mysqlnd就可以获取数据。 It puts the data into an associative array. 它将数据放入关联数组。

Mysqli - Bind results to an Array Mysqli-将结果绑定到数组

The solution from Daan is very nice! 大安的解决方案非常好! For this, i have to say thank you! 为此,我不得不说谢谢!

The point is, that i had, based on this, developed a class which works with mysqlnd, when it's there and Daan's alternative when it's missing. 关键是,基于此,我开发了一个与mysqlnd一起使用的类(当它存在时)和Daan的替代品(当它丢失时)。

I have built in fetchAssoc, fetchAllAssoc/Array, num_rows as methods. 我已经内置了fetchAssoc,fetchAllAssoc / Array,num_rows作为方法。 It's still in development and part of youphptube. 它仍在开发中,并且是youphptube的一部分。 This maybe also helps Ehab (whish for fetchAssoc). 这可能也有助于Ehab(对fetchAssoc表示敬意)。

https://github.com/DanielnetoDotCom/YouPHPTube/blob/master/objects/mysql_dal.php https://github.com/DanielnetoDotCom/YouPHPTube/blob/master/objects/mysql_dal.php

Usage: 用法:

    $sql = "SELECT * FROM users WHERE id = ?";
    $res = sqlDAL::readSql($sql,"i",array($users_id)); 
    $fullData = sqlDAL::fetchAllAssoc($res);
    // $data = sqlDAL::fetchAssoc($res); $data2 = sqlDAL::fetchAssoc($res); // or like this
    // $countedRows = sqlDAL::num_rows($res); // or like this
    sqlDAL::close($res);

I would made this as a sub-post to Daan's answer, but my reputation is too small. 我会将其作为Daan回答的子帖子,但我的声誉太小了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM